Spring Transactions - Propagation & Isolation - DevDummy

Latest

Views | Thoughts | Concepts | Techniques

Wednesday, May 03, 2023

Spring Transactions - Propagation & Isolation


Spring Transactions provide a way to manage transactions in Spring-based applications. Transactions help ensure data consistency and integrity by ensuring that a group of related database operations are treated as a single unit of work. Two important concepts in Spring Transactions are propagation and isolation.

Propagation describes how Spring Transactions handle transactions that are started from within other transactions. The propagation behavior determines whether the current transaction should be suspended, continued, or started anew. The possible propagation behaviors are:

  1. REQUIRED: This is the default propagation behavior in Spring. If a transaction already exists, the current method will join the existing transaction. Otherwise, a new transaction will be started.

  2. SUPPORTS: If a transaction already exists, the current method will join the existing transaction. Otherwise, the method will execute non-transactionally.

  3. MANDATORY: If a transaction already exists, the current method will join the existing transaction. Otherwise, an exception will be thrown.

  4. REQUIRES_NEW: A new transaction will always be started, and if a transaction already exists, it will be suspended until the new transaction completes.

  5. NOT_SUPPORTED: The current method will execute non-transactionally, and if a transaction already exists, it will be suspended until the method completes.

  6. NEVER: The current method will execute non-transactionally, and if a transaction already exists, an exception will be thrown.

  7. NESTED: A new transaction will be started within the current transaction. The nested transaction can be rolled back independently of the outer transaction.

Isolation describes how Spring Transactions handle concurrent access to the same database resources by multiple transactions. The isolation level determines the degree of isolation between transactions, and the possible isolation levels are:

  1. DEFAULT: The default isolation level of the database will be used.

  2. READ_UNCOMMITTED: Transactions can read uncommitted data from other transactions.

  3. READ_COMMITTED: Transactions can only read committed data from other transactions.

  4. REPEATABLE_READ: Transactions will always see the same data within a transaction, regardless of changes made by other transactions.

  5. SERIALIZABLE: Transactions will be completely isolated from each other, ensuring that all transactions execute as if they were executed serially.

It is important to choose the appropriate propagation behavior and isolation level for each transaction, based on the requirements of the application and the underlying data source.

References

  1. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Propagation.html
  2. https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Isolation.html
...

No comments:

Post a Comment