Spring Transaction详解 - Transaction Propagation模式

相关文档:Spring官方文档Javadoc

相关代码:github

在Spring官方文档中只对三种Transaction Propagation模式做了说明:

  1. Required
  2. RequiresNew
  3. Nested

并没有对另外四种做说明:

  1. SUPPORTS
  2. MANDATORY
  3. NOT_SUPPORTED
  4. NEVER

此外也没有对不同的Transaction Propagation嵌套组合的行为作一个详细说明,因此本文将会补全这一点。

测试方法的说明

有两张表FOO、BAR,这两张表都很简单,只有一个NAME VARCHAR2字段。针对这两张表分别有FooTxExecutorBarTxExecutor

FooTxExecutor有各种不同的实现,每个实现做的事情都相同,只是声明的Propagation模式不同。BarTxExecutor也是如此。

先看看BarTxAbstractExecutor做了什么:

  1. 插入数据
  2. 如果要求其抛出异常,则抛出一个FakeException

先看看FooTxAbstractExecutor做了什么:

  1. 插入数据
  2. 执行BarTxExecutor
  3. 捕获BarTxExecutor的异常
  4. 如果要求其抛出异常,则抛出一个FakeException

然后将不同Propagation模式的FooTxExecutorBarTxExecutor进行组合,获得测试结果。

测试结果

下表可以通过运行PropagationCombinationTest获得。

Foo modeFoo throw exBar modeBar throw exFoo success?Bar success?Exception
REQUIREDNREQUIREDNYY
REQUIREDYREQUIREDNNN
REQUIREDNREQUIREDYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
REQUIREDYREQUIREDYNN
REQUIREDNSUPPORTSNYY
REQUIREDYSUPPORTSNNN
REQUIREDNSUPPORTSYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
REQUIREDYSUPPORTSYNN
REQUIREDNMANDATORYNYY
REQUIREDYMANDATORYNNN
REQUIREDNMANDATORYYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
REQUIREDYMANDATORYYNN
REQUIREDNREQUIRES_NEWNYY
REQUIREDYREQUIRES_NEWNNY
REQUIREDNREQUIRES_NEWYYN
REQUIREDYREQUIRES_NEWYNN
REQUIREDNNOT_SUPPORTEDNYY
REQUIREDYNOT_SUPPORTEDNNY
REQUIREDNNOT_SUPPORTEDYYY
REQUIREDYNOT_SUPPORTEDYNY
REQUIREDNNEVERNYN
REQUIREDYNEVERNNN
REQUIREDNNEVERYYN
REQUIREDYNEVERYNN
REQUIREDNNESTEDNYY
REQUIREDYNESTEDNNN
REQUIREDNNESTEDYYN
REQUIREDYNESTEDYNN
SUPPORTSNREQUIREDNYY
SUPPORTSYREQUIREDNYY
SUPPORTSNREQUIREDYYN
SUPPORTSYREQUIREDYYN
SUPPORTSNSUPPORTSNYY
SUPPORTSYSUPPORTSNYY
SUPPORTSNSUPPORTSYYY
SUPPORTSYSUPPORTSYYY
SUPPORTSNMANDATORYNYN
SUPPORTSYMANDATORYNYN
SUPPORTSNMANDATORYYYN
SUPPORTSYMANDATORYYYN
SUPPORTSNREQUIRES_NEWNYY
SUPPORTSYREQUIRES_NEWNYY
SUPPORTSNREQUIRES_NEWYYN
SUPPORTSYREQUIRES_NEWYYN
SUPPORTSNNOT_SUPPORTEDNYY
SUPPORTSYNOT_SUPPORTEDNYY
SUPPORTSNNOT_SUPPORTEDYYY
SUPPORTSYNOT_SUPPORTEDYYY
SUPPORTSNNEVERNYY
SUPPORTSYNEVERNYY
SUPPORTSNNEVERYYY
SUPPORTSYNEVERYYY
SUPPORTSNNESTEDNYY
SUPPORTSYNESTEDNYY
SUPPORTSNNESTEDYYN
SUPPORTSYNESTEDYYN
MANDATORYNREQUIREDNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYREQUIREDNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNREQUIREDYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYREQUIREDYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNSUPPORTSNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYSUPPORTSNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNSUPPORTSYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYSUPPORTSYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNMANDATORYNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYMANDATORYNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNMANDATORYYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYMANDATORYYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNREQUIRES_NEWNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYREQUIRES_NEWNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNREQUIRES_NEWYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYREQUIRES_NEWYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNNOT_SUPPORTEDNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYNOT_SUPPORTEDNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNNOT_SUPPORTEDYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYNOT_SUPPORTEDYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNNEVERNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYNEVERNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNNEVERYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYNEVERYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNNESTEDNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYNESTEDNNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYNNESTEDYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
MANDATORYYNESTEDYNNorg.springframework.transaction.IllegalTransactionStateException No existing transaction found for transaction marked with propagation ‘mandatory’
REQUIRES_NEWNREQUIREDNYY
REQUIRES_NEWYREQUIREDNNN
REQUIRES_NEWNREQUIREDYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
REQUIRES_NEWYREQUIREDYNN
REQUIRES_NEWNSUPPORTSNYY
REQUIRES_NEWYSUPPORTSNNN
REQUIRES_NEWNSUPPORTSYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
REQUIRES_NEWYSUPPORTSYNN
REQUIRES_NEWNMANDATORYNYY
REQUIRES_NEWYMANDATORYNNN
REQUIRES_NEWNMANDATORYYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
REQUIRES_NEWYMANDATORYYNN
REQUIRES_NEWNREQUIRES_NEWNYY
REQUIRES_NEWYREQUIRES_NEWNNY
REQUIRES_NEWNREQUIRES_NEWYYN
REQUIRES_NEWYREQUIRES_NEWYNN
REQUIRES_NEWNNOT_SUPPORTEDNYY
REQUIRES_NEWYNOT_SUPPORTEDNNY
REQUIRES_NEWNNOT_SUPPORTEDYYY
REQUIRES_NEWYNOT_SUPPORTEDYNY
REQUIRES_NEWNNEVERNYN
REQUIRES_NEWYNEVERNNN
REQUIRES_NEWNNEVERYYN
REQUIRES_NEWYNEVERYNN
REQUIRES_NEWNNESTEDNYY
REQUIRES_NEWYNESTEDNNN
REQUIRES_NEWNNESTEDYYN
REQUIRES_NEWYNESTEDYNN
NOT_SUPPORTEDNREQUIREDNYY
NOT_SUPPORTEDYREQUIREDNYY
NOT_SUPPORTEDNREQUIREDYYN
NOT_SUPPORTEDYREQUIREDYYN
NOT_SUPPORTEDNSUPPORTSNYY
NOT_SUPPORTEDYSUPPORTSNYY
NOT_SUPPORTEDNSUPPORTSYYY
NOT_SUPPORTEDYSUPPORTSYYY
NOT_SUPPORTEDNMANDATORYNYN
NOT_SUPPORTEDYMANDATORYNYN
NOT_SUPPORTEDNMANDATORYYYN
NOT_SUPPORTEDYMANDATORYYYN
NOT_SUPPORTEDNREQUIRES_NEWNYY
NOT_SUPPORTEDYREQUIRES_NEWNYY
NOT_SUPPORTEDNREQUIRES_NEWYYN
NOT_SUPPORTEDYREQUIRES_NEWYYN
NOT_SUPPORTEDNNOT_SUPPORTEDNYY
NOT_SUPPORTEDYNOT_SUPPORTEDNYY
NOT_SUPPORTEDNNOT_SUPPORTEDYYY
NOT_SUPPORTEDYNOT_SUPPORTEDYYY
NOT_SUPPORTEDNNEVERNYY
NOT_SUPPORTEDYNEVERNYY
NOT_SUPPORTEDNNEVERYYY
NOT_SUPPORTEDYNEVERYYY
NOT_SUPPORTEDNNESTEDNYY
NOT_SUPPORTEDYNESTEDNYY
NOT_SUPPORTEDNNESTEDYYN
NOT_SUPPORTEDYNESTEDYYN
NEVERNREQUIREDNYY
NEVERYREQUIREDNYY
NEVERNREQUIREDYYN
NEVERYREQUIREDYYN
NEVERNSUPPORTSNYY
NEVERYSUPPORTSNYY
NEVERNSUPPORTSYYY
NEVERYSUPPORTSYYY
NEVERNMANDATORYNYN
NEVERYMANDATORYNYN
NEVERNMANDATORYYYN
NEVERYMANDATORYYYN
NEVERNREQUIRES_NEWNYY
NEVERYREQUIRES_NEWNYY
NEVERNREQUIRES_NEWYYN
NEVERYREQUIRES_NEWYYN
NEVERNNOT_SUPPORTEDNYY
NEVERYNOT_SUPPORTEDNYY
NEVERNNOT_SUPPORTEDYYY
NEVERYNOT_SUPPORTEDYYY
NEVERNNEVERNYY
NEVERYNEVERNYY
NEVERNNEVERYYY
NEVERYNEVERYYY
NEVERNNESTEDNYY
NEVERYNESTEDNYY
NEVERNNESTEDYYN
NEVERYNESTEDYYN
NESTEDNREQUIREDNYY
NESTEDYREQUIREDNNN
NESTEDNREQUIREDYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
NESTEDYREQUIREDYNN
NESTEDNSUPPORTSNYY
NESTEDYSUPPORTSNNN
NESTEDNSUPPORTSYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
NESTEDYSUPPORTSYNN
NESTEDNMANDATORYNYY
NESTEDYMANDATORYNNN
NESTEDNMANDATORYYNNorg.springframework.transaction.UnexpectedRollbackException Transaction rolled back because it has been marked as rollback-only
NESTEDYMANDATORYYNN
NESTEDNREQUIRES_NEWNYY
NESTEDYREQUIRES_NEWNNY
NESTEDNREQUIRES_NEWYYN
NESTEDYREQUIRES_NEWYNN
NESTEDNNOT_SUPPORTEDNYY
NESTEDYNOT_SUPPORTEDNNY
NESTEDNNOT_SUPPORTEDYYY
NESTEDYNOT_SUPPORTEDYNY
NESTEDNNEVERNYN
NESTEDYNEVERNNN
NESTEDNNEVERYYN
NESTEDYNEVERYNN
NESTEDNNESTEDNYY
NESTEDYNESTEDNNN
NESTEDNNESTEDYYN
NESTEDYNESTEDYNN
    原文作者:chanjarster
    原文地址: https://segmentfault.com/a/1190000008200613
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞