Spring seven kinds of propagation behavior Affairs

Transaction propagation behavior

"Transaction propagation behavior" is described: When a transaction method calls another method, how the transaction method?

It is to create a new transaction? Discard affairs? Or added to an existing transaction it?

For these situations, Spring Framework defines seven transaction propagation behavior, developers can select the appropriate propagation behavior based on actual business scenarios.

Seven kinds of transaction propagation behavior

Spring which is defined in a class enumeration Propagation, the are as follows:

  • REQUIRED
    If the transaction is not currently open, you open a new transaction, if the transaction is currently open, joined the transaction, the default behavior.
  • SUPPORTS
    If the current open transaction, joined the transaction, otherwise non-transactional.
  • MANDATORY
    if the current open transaction, joined the transaction, or an exception is thrown. To put it plainly, force the transaction, does not allow non-transactional execution.
  • REQUIRES_NEW
    always create a new transaction, if the transaction is currently open, the current transaction suspension.
  • NOT_SUPPORTED
    forced to perform non-transactional, if the current transaction will open again suspend the implementation of the current transaction.
  • NEVER
    non-transactional execution, if the current open transaction exception is thrown.
  • NESTED
    If the current open transaction is executed in a nested transaction, or open a transaction execution.

special reminder

Spring's transaction is based on the proxy class achieved through AOP, if you want to propagate the transaction to take effect, you must call the method by Spring generated proxy class, Spring determine whether to open the transaction in enhancing proxy class, in direct call the same class Spring method itself is unable to help us open the transaction!

As an example, transaction propagation behavior will not take effect:
Here Insert Picture Description

Call different Service, because the proxy class to call, so the spread of the transaction will take effect:
Here Insert Picture Description

Seven kinds of communication behavior example

REQUIRED

Spring默认的事务传播行为,也是用的最多的。
如果当前没有开启事务,就开启一个新事务,如果当前开启了事务,就加入该事务,默认行为。

Here Insert Picture Description
如上图,save没有开启事务,insert不会回滚,delete开启了事务,delete会回滚。
如果save开启了事务,则delete不会开启新事务,会加入到save的事务中,insert和delete都会回滚。

SUPPORTS

如果当前开启了事务,就加入该事务,否则非事务执行。
Here Insert Picture Description
如上图,save没有开启事务,delete也不会开启事务,insert和delete操作均不会回滚。
如果save开启了事务,delete会加入到save的事务中,则insert和delete均会回滚。

MANDATORY

如果当前开启了事务,就加入该事务,否则抛出异常。说白了,强制事务,不允许非事务执行。
Here Insert Picture Description
如上图,save没有开启事务,执行delete()时会抛出异常:
Here Insert Picture Description
insert成功,delete操作没有执行。
如果save开启了事务,则delete()加入到事务中,不会抛异常。

REQUIRES_NEW

如果当前没有开启事务,则开启一个事务,事务的执行。
如果当前开启了事务,则将当前事务挂起,开启一个新事务,事务的执行。
外层事务和内层事务是互相隔离的,内层事务的提交或回滚不影响外层事务,外层事务的提交或回滚也不影响内层事务。

需要捕获内层方法异常,否则内层方法抛异常,会影响外层事务回滚。

Here Insert Picture Description
如上图,save开启了事务A,执行delete()时,会将save的事务A挂起,并开启新事务B,delete()执行成功,事务B提交,然后恢复事务A,接着抛出异常,事务A回滚,但是事务B已经提交不会受到影响。
最终delete操作成功,insert操作被回滚。

从控制台的日志也能看见其运行过程:
Here Insert Picture Description

NOT_SUPPORTED

强制以非事务执行,如果当前开启了事务就将当前事务挂起再执行。
Here Insert Picture Description
如上图,save开启了事务A,执行delete()时会挂起事务A,然后以非事务的方式执行delete并提交,然后恢复事务A,由于save抛出异常,insert回滚。
结果:insert操作回滚,delete操作成功。

NEVER

非事务执行,如果当前开启了事务则抛出异常。
Here Insert Picture Description
如上图,由于save开启了事务,导致delete抛出异常:
Here Insert Picture Description
如果save没有开启事务,则save和delete都会以非事务执行。

NESTED

如果当前没有开启事务,则开启一个新事务,事务的执行。
否则,在当前事务中以嵌套事务的方式执行。
以嵌套事务执行前,会创建一个保存点savePoint,如果方法异常,则回滚到该savePoint,否则等待和外层事务一起Commit。
Here Insert Picture Description

关于Spring的七种事务传播行为都介绍完了,根据实际应用场景选择需要的事务传播行为即可。

REQUIRES_NEW和NESTED的区别

REQUIRES_NEW和NESTED这两种传播行为比较容易搞混,但其实它俩有本质区别。

在当前已经开启事务的情况下:

REQUIRES_NEW会开启一个和外层事务真正隔离的内层事务,内层方法执行完后,内层事务就已经提交或回滚了,和外层事务是完全隔离的,互不影响的。内层事务结束后,才会恢复外层事务,外层事务继续执行。

NESTED nature and does not open a new transaction, but in a way to create savePoint nested execution, regardless of whether the transaction rollback or commit the inner layer, the outer layer of the final by the transactions to be executed together.

Published 100 original articles · won praise 23 · views 90000 +

Guess you like

Origin blog.csdn.net/qq_32099833/article/details/104010666