Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Good question, although not a trivial one to answer. </p> <p><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/transaction/annotation/Propagation.html" rel="noreferrer"><strong>Propagation</strong></a></p> <p>Defines how transactions relate to each other. Common options</p> <ul> <li><code>Required</code>: Code will always run in a transaction. Create a new transaction or reuse one if available.</li> <li><code>Requires_new</code>: Code will always run in a new transaction. Suspend current transaction if one exist. </li> </ul> <p><a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/transaction/TransactionDefinition.html" rel="noreferrer"><strong>Isolation</strong></a></p> <p>Defines the data contract between transactions. </p> <ul> <li><code>Read Uncommitted</code>: Allows dirty reads</li> <li><code>Read Committed</code>: Does not allow dirty reads</li> <li><code>Repeatable Read</code>: If a row is read twice in the same transaciton, result will always be the same</li> <li><code>Serializable</code>: Performs all transactions in a sequence</li> </ul> <p>The different levels have different performance characteristics in a multi threaded application. I think if you understand the <code>dirty reads</code> concept you will be able to select a good option. </p> <hr> <p>Example when a dirty read can occur</p> <pre><code> thread 1 thread 2 | | write(x) | | | | read(x) | | rollback | v v value (x) is now dirty (incorrect) </code></pre> <p>So a sane default (if such can be claimed) could be <code>Read Comitted</code>, which only lets you read values which have already been comitted by other running transactions, in combination with a propagation level of <code>Required</code>. Then you can work from there if you application has other needs. </p> <hr> <p>A practical example where a new transaction will always be created when entering the <code>provideService</code> routine and completed when leaving. </p> <pre><code>public class FooService { private Repository repo1; private Repository repo2; @Transactional(propagation=Propagation.REQUIRES_NEW) public void provideService() { repo1.retrieveFoo(); repo2.retrieveFoo(); } } </code></pre> <p>Had we used <code>Required</code> instead the transaction <a href="https://stackoverflow.com/questions/2754160/question-about-spring-transaction-propagation">will remain open</a> if the transaction was already open when entering the routine. Note also that the result of a <code>rollback</code> could be different as several executions could take part in the same transaction. </p> <hr> <p>We can easily verify the behaviour with a test and see how results differ with propagation levels</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:/fooService.xml") public class FooServiceTests { private @Autowired TransactionManager transactionManager; private @Autowired FooService fooService; @Test public void testProvideService() { TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); fooService.provideService(); transactionManager.rollback(status); // assert repository values are unchanged ... } </code></pre> <p>With a propagation level of</p> <ul> <li><p><code>Requires new</code> we would expect <code>fooService.provideService()</code> was <em>NOT</em> rolled back since it created it's own sub-transaction. </p></li> <li><p><code>Required</code> we would expect everything was rolled back and backing store unchanged. </p></li> </ul>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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