Spring independent transaction and nested transaction

Types of transaction propagation in Spring:

  • PROPAGATION_REQUIRED - Support the current transaction. If there is no transaction currently, create a new transaction. (default)
  • PROPAGATION_SUPPORTS-supports the current transaction, if there is no transaction currently, it is executed in a non-transactional manner.
  • PROPAGATION_MANDATORY - supports the current transaction, if there is no current transaction, throw an exception.
  • PROPAGATION_REQUIRES_NEW - Create a new transaction. If there is a transaction currently, suspend the current transaction.
  • PROPAGATION_NOT_SUPPORTED - The operation is performed in a non-transactional manner. If there is a transaction currently, the current transaction is suspended.
  • PROPAGATION_NEVER - Execute in a non-transactional manner, if there is currently a transaction, an exception will be thrown.
  • PROPAGATION_NESTED - If a transaction currently exists, it will be executed within a nested transaction. If there is no transaction currently, proceed similar to PROPAGATION_REQUIRED.

 

The difference between PROPAGATION_NESTED and  PROPAGATION_REQUIRES_NEW  :

When PROPAGATION_REQUIRES_NEW is used, the inner transaction and the outer transaction are like two independent transactions. Once the inner transaction is committed, the outer transaction cannot roll back it. The two transactions do not affect each other. Two transactions are not a true nested transaction. At the same time, it needs the support of the JTA transaction manager. Start a new "internal" transaction that does not depend on the environment. This transaction will be fully committed or rolled back without relying on external transactions. It has its own isolation scope, its own locks, etc. When the internal transaction starts to execute When the external transaction is suspended, the external transaction will continue to execute when the internal transaction ends.

When PROPAGATION_NESTED is used, the rollback of the outer transaction can cause the rollback of the inner transaction. The exception of the inner transaction does not cause the rollback of the outer transaction, it is a true nested transaction. When DataSourceTransactionManager uses savepoint to support PROPAGATION_NESTED, it requires JDBC 3.0 or higher driver and 1.4 or higher JDK version support. Other JTA TrasactionManager implementations may have different support methods. PROPAGATION_NESTED starts a "nested" transaction, which is a real sub-transaction of an existing transaction. When the latent transaction starts to execute, it will get a savepoint. If the nested transaction fails, we will roll back to this savepoint. The latent transaction is part of the external transaction, and it will be committed only after the external transaction ends.

 

Summary: The biggest difference between PROPAGATION_REQUIRES_NEW and PROPAGATION_NESTED is that PROPAGATION_REQUIRES_NEW is a completely new transaction, while PROPAGATION_NESTED is a sub-transaction of an external transaction. If the external transaction commits, the latent transaction will also be committed. This rule also applies to roll back.

Guess you like

Origin blog.csdn.net/qq_36807862/article/details/105995045