Transactional Isolation And Propagation: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
(Created page with "==[https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/transaction/annotation/Propagation.html Propagation]== Defines how transactions relate to each oth...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
==[https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/transaction/annotation/Propagation.html Propagation]==
==Summery==
===[https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/transaction/annotation/Propagation.html Propagation]===


Defines how transactions relate to each other. Common options:
Defines how transactions relate to each other. Common options:
Line 6: Line 7:
* <code>Requires_new</code>: Code will always run in a new transaction. Suspends the current transaction if one exists.
* <code>Requires_new</code>: Code will always run in a new transaction. Suspends the current transaction if one exists.


==[https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/transaction/TransactionDefinition.html Isolation]==
===[https://docs.spring.io/spring/docs/5.0.x/javadoc-api/org/springframework/transaction/TransactionDefinition.html Isolation]===
Defines the data contract between transactions.
Defines the data contract between transactions.


Line 14: Line 15:
* <code>Serializable</code>: Performs all transactions in a sequence.
* <code>Serializable</code>: Performs all transactions in a sequence.
The different levels have different performance characteristics in a <code>multi-threaded</code> application. Those who understand the <code>dirty reads</code> concept they are able to be select a good option.
The different levels have different performance characteristics in a <code>multi-threaded</code> application. Those who understand the <code>dirty reads</code> concept they are able to be select a good option.
===Example===
When a dirty read can occur:
  thread 1  thread 2     
      |        |
    write(x)    |
      |        |
      |        read(x)
      |        |
    rollback    |
      v        v
          value (x) is now dirty (incorrect)
==Isolation Level==
defines how the changes made to some data repository by one transaction affect other simultaneous concurrent transactions, and also how and when that changed data becomes available to other transactions. When we define a transaction using the Spring framework we are also able to configure in which isolation level that same transaction will be executed.
<source lang="java">
@Transactional(isolation=Isolation.READ_COMMITTED)
public void someTransactionalMethod(Object obj) {
}
</source>
* <code>READ_UNCOMMITTED</code> isolation level states that a transaction may read data that is still uncommitted by other transactions.
* <code>READ_COMMITTED</code> isolation level states that a transaction can't read data that is not yet committed by other transactions.
* <code>REPEATABLE_READ</code> isolation level states that if a transaction reads one record from the database multiple times the result of all those reading operations must always be the same.
* <code>SERIALIZABLE</code> isolation level is the most restrictive of all isolation levels. Transactions are executed with locking at all levels (read, range and write locking) so they appear as if they were executed in a serialized way.
==Propagation==
Is the ability to decide how the business methods should be encapsulated in both logical or physical transactions.
* <code>REQUIRED</code> behavior means that the same transaction will be used if there is an already opened transaction in the current bean method execution context.
* <code>REQUIRES_NEW</code> behavior means that a new physical transaction will always be created by the container.
* <code>NESTED</code> behavior makes nested Spring transactions to use the same physical transaction but sets savepoints between nested invocations so inner transactions may also rollback independently of outer transactions.
* <code>MANDATORY</code> behavior states that an existing opened transaction must already exist. If not an exception will be thrown by the container.
* <code>NEVER</code> behavior states that an existing opened transaction must not already exist. If a transaction exists an exception will be thrown by the container.
* <code>NOT_SUPPORTED</code> behavior will execute outside of the scope of any transaction. If an opened transaction already exists it will be paused.
* <code>SUPPORTS</code> behavior will execute in the scope of a transaction if an opened transaction already exists. If there isn't an already opened transaction the method will execute anyway but in a non-transactional way.


==References==
==References==
* [https://stackoverflow.com/questions/8490852 Spring Transactional Isolation & Propagation]
* [https://stackoverflow.com/questions/8490852 Spring Transactional Isolation & Propagation]
* [https://www.byteslounge.com/tutorials/spring-transaction-isolation-tutorial Spring transaction isolation level tutorial]
* [https://www.byteslounge.com/tutorials/spring-transaction-isolation-tutorial Spring transaction isolation level tutorial]
* [https://mybatis.org/spring/transactions.html MyBatis Spring Transactions]

Latest revision as of 20:56, 18 November 2019

Summery

Propagation

Defines how transactions relate to each other. Common options:

  • Required: Code will always run in a transaction. Creates a new transaction or reuses one if available.
  • Requires_new: Code will always run in a new transaction. Suspends the current transaction if one exists.

Isolation

Defines the data contract between transactions.

  • Read Uncommitted: Allows dirty reads.
  • Read Committed: Does not allow dirty reads.
  • Repeatable Read: If a row is read twice in the same transaction, the result will always be the same.
  • Serializable: Performs all transactions in a sequence.

The different levels have different performance characteristics in a multi-threaded application. Those who understand the dirty reads concept they are able to be select a good option.

Example

When a dirty read can occur:

 thread 1   thread 2      
     |         |
   write(x)    |
     |         |
     |        read(x)
     |         |
   rollback    |
     v         v 
          value (x) is now dirty (incorrect)

Isolation Level

defines how the changes made to some data repository by one transaction affect other simultaneous concurrent transactions, and also how and when that changed data becomes available to other transactions. When we define a transaction using the Spring framework we are also able to configure in which isolation level that same transaction will be executed.

@Transactional(isolation=Isolation.READ_COMMITTED)
public void someTransactionalMethod(Object obj) {

}
  • READ_UNCOMMITTED isolation level states that a transaction may read data that is still uncommitted by other transactions.
  • READ_COMMITTED isolation level states that a transaction can't read data that is not yet committed by other transactions.
  • REPEATABLE_READ isolation level states that if a transaction reads one record from the database multiple times the result of all those reading operations must always be the same.
  • SERIALIZABLE isolation level is the most restrictive of all isolation levels. Transactions are executed with locking at all levels (read, range and write locking) so they appear as if they were executed in a serialized way.

Propagation

Is the ability to decide how the business methods should be encapsulated in both logical or physical transactions.

  • REQUIRED behavior means that the same transaction will be used if there is an already opened transaction in the current bean method execution context.
  • REQUIRES_NEW behavior means that a new physical transaction will always be created by the container.
  • NESTED behavior makes nested Spring transactions to use the same physical transaction but sets savepoints between nested invocations so inner transactions may also rollback independently of outer transactions.
  • MANDATORY behavior states that an existing opened transaction must already exist. If not an exception will be thrown by the container.
  • NEVER behavior states that an existing opened transaction must not already exist. If a transaction exists an exception will be thrown by the container.
  • NOT_SUPPORTED behavior will execute outside of the scope of any transaction. If an opened transaction already exists it will be paused.
  • SUPPORTS behavior will execute in the scope of a transaction if an opened transaction already exists. If there isn't an already opened transaction the method will execute anyway but in a non-transactional way.

References