Note that there are some explanatory texts on larger screens.

plurals
  1. POGrails service transactional behaviour
    primarykey
    data
    text
    <p>In a Grails app, the default behaviour of service methods is that they are transactional and the transaction is automatically rolled-back if an unchecked exception is thrown. However, in Groovy one is not forced to handle (or rethrow) checked exceptions, so there's a risk that if a service method throws a checked exception, the transaction will not be rolled back. On account of this, it seems advisable to annotate every Grails service class</p> <pre><code>@Transactional(rollbackFor = Throwable.class) class MyService { void writeSomething() { } } </code></pre> <p>Assume I have other methods in <code>MyService</code>, one of which only reads the DB, and the other doesn't touch the DB, are the following annotations correct?</p> <pre><code>@Transactional(readOnly = true) void readSomething() {} // Maybe this should be propagation = Propagation.NOT_SUPPORTED instead? @Transactional(propagation = Propagation.SUPPORTS) void dontReadOrWrite() {} </code></pre> <p>In order to answer this question, I guess you'll need to know what my intention is:</p> <ul> <li>If an exception is thrown from any method and there's a transaction in progress, it will be rolled back. For example, if <code>writeSomething()</code> calls <code>dontReadOrWrite()</code>, and an exception is thrown from the latter, the transaction started by the former will be rolled back. I'm assuming that the <code>rollbackFor</code> class-level attribute is inherited by individual methods unless they explicitly override it.</li> <li>If there's no transaction in progress, one will not be started for methods like <code>dontReadOrWrite</code></li> <li>If no transaction is in progress when <code>readSomething()</code> is called, a read-only transaction will be started. If a read-write transaction is in progress, it will participate in this transaction.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload