`
gitzhangyl
  • 浏览: 18423 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring事务的传播行为

阅读更多

 

Spring事务的传播行为(Propagation Behavior)在接口TransactionDefinition中定义,共七种传播行为。

 

PROPAGATION_REQUIRED

org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED
org.springframework.transaction.annotation.Propagation.REQUIRED

 

Support a current transaction; create a new one if none exists. Analogous to the EJB transaction attribute of the same name.

 

支持当前的事务,如果不存在则创建一个新事务。类似于同名的EJB事务属性。

 

This is typically the default setting of a transaction definition, and typically defines a transaction synchronization scope.

 

这是事务定义的默认设置,并且定义了事务同步作用域。

 

PROPAGATION_SUPPORTS

org.springframework.transaction.TransactionDefinition.PROPAGATION_SUPPORTS
org.springframework.transaction.annotation.Propagation.SUPPORTS

 

Support a current transaction; execute non-transactionally if none exists. Analogous to the EJB transaction attribute of the same name.

 

支持当前的事务,如果不存在则以非事务的方式执行。类似于同名的EJB事务属性。

 

NOTE: For transaction managers with transaction synchronization, PROPAGATION_SUPPORTS is slightly different from no transaction at all, as it defines a transaction scope that synchronization might apply to. As a consequence, the same resources (a JDBC Connection, a Hibernate Session, etc) will be shared for the entire specified scope. Note that the exact behavior depends on the actual synchronization configuration of the transaction manager!

 

注意:对于带有事务同步的事务管理器,PROPAGATION_SUPPORTS与没有事务稍有不同,因为它定义了同步可能会应用到的事务作用域。因而,对于整个指定的作用域,相同的资源(JDBC Connection、Hibernate Session等)将是共享的。注意,确切的行为依赖于事务管理器实际的同步配置。

 

In general, use PROPAGATION_SUPPORTS with care! In particular, do not rely on PROPAGATION_REQUIRED or PROPAGATION_REQUIRES_NEW within a PROPAGATION_SUPPORTS scope (which may lead to synchronization conflicts at runtime). If such nesting is unavoidable, make sure to configure your transaction manager appropriately (typically switching to "synchronization on actual transaction").

 

一般,使用PROPAGATION_SUPPORTS要谨慎!尤其是,在PROPAGATION_SUPPORTS作用域内不要依赖PROPAGATION_REQUIRED或PROPAGATION_REQUIRES_NEW,这可能会导致运行时事务同步冲突。如果这种嵌套是不可避免的,要确保恰当地配置事务管理器(通常切换到"synchronization on actual transaction")。

 

PROPAGATION_MANDATORY

org.springframework.transaction.TransactionDefinition.PROPAGATION_MANDATORY
org.springframework.transaction.annotation.Propagation.MANDATORY

 

Support a current transaction; throw an exception if no current transaction exists. Analogous to the EJB transaction attribute of the same name.

 

支持当前的事务,如果不存在则抛出异常。类似于同名的EJB事务属性。

 

Note that transaction synchronization within a PROPAGATION_MANDATORY scope will always be driven by the surrounding transaction.

 

注意:在PROPAGATION_MANDATORY作用域内的事务同步总是由外围的事务驱动。

 

PROPAGATION_REQUIRES_NEW

org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRES_NEW
org.springframework.transaction.annotation.Propagation.REQUIRES_NEW

 

Create a new transaction, suspending the current transaction if one exists. Analogous to the EJB transaction attribute of the same name.

 

创建新事务,如果存在当前事务则挂起。类似于同名的EJB事务属性。

 

NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available it to it (which is server-specific in standard J2EE).

 

注意:实际的事务挂起不是对所有的事务管理器都是开箱即用。尤其是应用到JtaTransactionManager,它要求javax.transaction.TransactionManager来使这个可用。

 

A PROPAGATION_REQUIRES_NEW scope always defines its own transaction synchronizations. Existing synchronizations will be suspended and resumed appropriately.

 

PROPAGATION_REQUIRES_NEW作用域总是定义自己的事务同步。已存在的同步将被恰当地挂起和恢复。

 

PROPAGATION_NOT_SUPPORTED

org.springframework.transaction.TransactionDefinition.PROPAGATION_NOT_SUPPORTED
org.springframework.transaction.annotation.Propagation.NOT_SUPPORTED

 

Do not support a current transaction; rather always execute non-transactionally. Analogous to the EJB transaction attribute of the same name.

 

不支持当前事务,总是以非事务方式执行。类似于同名的EJB事务属性。

 

NOTE: Actual transaction suspension will not work out-of-the-box on all transaction managers. This in particular applies to org.springframework.transaction.jta.JtaTransactionManager, which requires the javax.transaction.TransactionManager to be made available it to it (which is server-specific in standard J2EE).

 

注意:实际的事务挂起不是对所有的事务管理器都是开箱即用。尤其是应用到JtaTransactionManager,它要求javax.transaction.TransactionManager来使这个可用。

 

Note that transaction synchronization is not available within a PROPAGATION_NOT_SUPPORTED scope. Existing synchronizations will be suspended and resumed appropriately.

 

在PROPAGATION_NOT_SUPPORTED作用域内,事务同步是不可用的。已存在的同步将被恰当地挂起和恢复。

 

PROPAGATION_NEVER

org.springframework.transaction.TransactionDefinition.PROPAGATION_NEVER
org.springframework.transaction.annotation.Propagation.NEVER

 

Do not support a current transaction; throw an exception if a current transaction exists. Analogous to the EJB transaction attribute of the same name.

 

不支持当前事务,如果存在当前事务则抛出异常。类似于同名的EJB事务属性。

 

Note that transaction synchronization is not available within a PROPAGATION_NEVER scope.

 

注意:在PROPAGATION_NEVER作用域内,事务同步是不可用的。

 

PROPAGATION_NESTED

org.springframework.transaction.TransactionDefinition.PROPAGATION_NESTED
org.springframework.transaction.annotation.Propagation.NESTED

 

Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB.

 

如果存在当前事务,则在嵌套事务中执行,行为像PROPAGATION_REQUIRED。EJB中没有类似的事务属性。

 

NOTE: Actual creation of a nested transaction will only work on specific transaction managers. Out of the box, this only applies to the JDBC org.springframework.jdbc.datasource.DataSourceTransactionManager when working on a JDBC 3.0 driver. Some JTA providers might support nested transactions as well.

 

注意:嵌套事务的实际创建仅在特定事务管理器上起作用。仅当在JDBC 3.0驱动上工作时,应用于JDBC DataSourceTransactionManager。一些JTA提供商可能也支持嵌套事务。

 

总结

TransactionDefinition接口 Propagation枚举 描述
PROPAGATION_REQUIRED REQUIRED 支持当前的事务,如果不存在则创建一个新事务。
PROPAGATION_SUPPORTS SUPPORTS 支持当前的事务,如果不存在则以非事务的方式执行。
PROPAGATION_MANDATORY MANDATORY 支持当前的事务,如果不存在则抛出异常。
PROPAGATION_REQUIRES_NEW REQUIRES_NEW 创建新事务,如果存在当前事务则挂起。
PROPAGATION_NOT_SUPPORTED NOT_SUPPORTED 不支持当前事务,总是以非事务方式执行。
PROPAGATION_NEVER NEVER 不支持当前事务,如果存在当前事务则抛出异常。
PROPAGATION_NESTED NESTED 如果存在当前事务,则在嵌套事务中执行。

 

 

分享到:
评论

相关推荐

    浅谈Spring事务传播行为实战

    主要介绍了浅谈Spring事务传播行为实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

    Spring事务传播行为问题解决

    主要介绍了Spring事务传播行为问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    Spring在Transaction事务传播行为种类

    Spring在Transaction事务传播行为种类,希望对大家有所帮助

    SPRING事务传播特性&事务隔离级别

    事务传播特性&事务隔离级别 详细的事务传播特性&事务隔离级别

    spring常用数据库事务传播属性和事务隔离级别1

    事务的属性:1.Propagation:用来设置事务的传播行为事务的传播行为:一个方法运行在了一个开启了事务的方法中时,当前方法是使用原来的事务还是开启了一个新

    Spring事务传播机制.docx

    【Spring五个事务隔离级别和七个事务传播行为】 数据库事务和Spring事务是一般面试都会被提到,很多朋友写惯了代码,很少花时间去整理归纳这些东西,结果本来会的东西,居然吞吞吐吐答不上来。 下面是我收集到一些...

    Spring的7 种事务传播行为.pdf

    Spring的7 种事务传播行为.pdfSpring的7 种事务传播行为.pdf

    Spring的7 种事务传播行为.docx

    Spring的7 种事务传播行为.docxSpring的7 种事务传播行为.docx

    Spring中事务传播行为的介绍

    今天小编就为大家分享一篇关于Spring中事务传播行为的介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    Spring框架+Spring中的事务

    Spring事务传播机制可以通过Propagation枚举类中的不同值来指定,共包括七种不同的传播行为。具体来说,Spring事务传播机制包括以下七种: REQUIRED:如果当前没有事务,则创建一个新的事务;如果当前已经存在事务,...

    深入理解Spring的事务传播行为

    spring支持7种事务传播行为,确定客户端和被调用端的事务边界(说得通俗一点就是多个具有事务控制的service的相互调用时所形成的复杂的事务边界控制),这篇文章主要给大家介绍了关于Spring事务传播行为的相关资料,...

    Spring.NET学习笔记17——事务传播行为(基础篇)代码下载

    Spring.NET事务配置模板。 原文出处:http://www.cnblogs.com/GoodHelper/archive/2009/11/16/SpringNet_Transaction.html

    深入理解Spring事务的传播行为

    Spring在TransactionDefinition...下面这篇文章主要给大家介绍了关于Spring事务传播行为的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    Spring事务测试题及原理

    此ppt中前半部分通过spring事务的60道题的测试,摸底对事务的掌握情况,后半部分,对spring中的事务属性(传播行为、隔离级别、回滚规则、事务超时、是否只读)进行说明

    Spring中的事务传播行为示例详解

    主要给大家介绍了关于Spring中事务传播行为的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

    Spring事务传播属性和隔离级别详细介绍

    主要介绍了Spring事务传播属性和隔离级别详细介绍,同时涉及传播行为介绍,超时设置等相关内容,需要的朋友可以参考下。

    通过实际案例摸清楚Spring事务传播的行为.docx

    1.DAO接口与实现类 /** * 用户数据访问层(DAO)接口 */ public interface UserDAO { // 查找所有用户 List<User> findAll(); // 根据id查找用户 User findById(Long id) throws SQLException;...

    Spring事务之7种传播行1

    【Spring学习34】Spring事务(4):事务属性之7种传播行为 - 程序老兵的博客 - CSDN博客Python工程师首页博客学院下载论坛图文课问答商城

    深入理解Spring声明式事务:源码分析与应用实践

    此外,Spring事务管理器支持多种类型的事务策略,包括不同的传播行为和隔离级别,允许开发者根据具体业务场景选择最合适的事务管理策略。深入理解Spring声明式事务的工作原理,不仅能帮助开发者更高效地使用Spring...

Global site tag (gtag.js) - Google Analytics