Spring的 事务传播行为类型

先看下Spring的 事务传播行为类型
事务传播行为类型说明
PROPAGATION_REQUIRED如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是 最常见的选择。
PROPAGATION_SUPPORTS支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与 PROPAGATION_REQUIRED 类似的操作。
事务传播行为类型
说明
PROPAGATION_REQUIRED
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是 最常见的选择。
PROPAGATION_SUPPORTS
支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY
使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW
新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER
以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与 PROPAGATION_REQUIRED 类似的操作。
当使用 PROPAGATION_NESTED 时, 底层的数据源必须基于 JDBC 3.0 ,并且实现者需要支持保存点事务机制。
readOnly       事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。这 是一个最优化提示 。在一些情况下,一些事务策略能够起到显著的最优化效果,例如在使用Object/Relational映射工具 (如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。
Timeout
在事务属性中还有定义“timeout”值的选项,指定事务超时为几秒。在JTA中,这将被简单地传递到J2EE服务器的事务协调程序,并据此得到相应的 解释。
在xml中的设置应该是 timeout_11 表示超时为11秒。。
为什么呢。。看下面的源码可知。。
Java代码
/**
* PropertyEditor for TransactionAttribute objects. Takes Strings of form
* <p><code>PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2</code>
* <p>where only propagation code is required. For example:
* <p><code>PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>
* <p>The tokens can be in <strong>any</strong> order. Propagation and isolation codes
* must use the names of the constants in the TransactionDefinition class. Timeout values
* are in seconds. If no timeout is specified, the transaction manager will apply a default
* timeout specific to the particular transaction manager.
* <p>A "+" before an exception name substring indicates that
* transactions should commit even if this exception is thrown;
* a "-" that they should roll back.
* @author Rod Johnson
* @author Juergen Hoeller
* @since 24.04.2003
* @see org.springframework.transaction.TransactionDefinition
* @see org.springframework.core.Constants
*/
public class TransactionAttributeEditor extends PropertyEditorSupport {
/**
* Format is PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2.
* Null or the empty string means that the method is non transactional.
* @see java.beans.PropertyEditor#setAsText(java.lang.String)
*/
public void setAsText(String s) throws IllegalArgumentException {
if (s == null || "".equals(s)) {
setValue(null);
else {
// tokenize it with ","
String[] tokens = StringUtils.commaDelimitedListToStringArray(s);
RuleBasedTransactionAttribute attr = new RuleBasedTransactionAttribute();
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_PROPAGATION)) {
attr.setPropagationBehaviorName(token);
else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ISOLATION)) {
attr.setIsolationLevelName(token);
else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_TIMEOUT)) {
String value = token.substring(DefaultTransactionAttribute.PREFIX_TIMEOUT.length());
attr.setTimeout(Integer.parseInt(value));
else if (token.equals(RuleBasedTransactionAttribute.READ_ONLY_MARKER)) {
attr.setReadOnly(true);
else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_COMMIT_RULE)) {
attr.getRollbackRules().add(new NoRollbackRuleAttribute(token.substring(1)));
else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ROLLBACK_RULE)) {
attr.getRollbackRules().add(new RollbackRuleAttribute(token.substring(1)));
else {
throw new IllegalArgumentException("Illegal transaction attribute token: [" + token + "]");
setValue(attr);
Java代码
/**
* PropertyEditor for TransactionAttribute objects. Takes Strings of form
* <p><code>PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2</code>
* <p>where only propagation code is required. For example:
* <p><code>PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>
* <p>The tokens can be in <strong>any</strong> order. Propagation and isolation codes
* must use the names of the constants in the TransactionDefinition class. Timeout values
* are in seconds. If no timeout is specified, the transaction manager will apply a default
* timeout specific to the particular transaction manager.
* <p>A "+" before an exception name substring indicates that
* transactions should commit even if this exception is thrown;
* a "-" that they should roll back.
* @author Rod Johnson
* @author Juergen Hoeller
* @since 24.04.2003
* @see org.springframework.transaction.TransactionDefinition
* @see org.springframework.core.Constants
*/
public class TransactionAttributeEditor extends PropertyEditorSupport {
/**
* Format is PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2.
* Null or the empty string means that the method is non transactional.
* @see java.beans.PropertyEditor#setAsText(java.lang.String)
*/
public void setAsText(String s) throws IllegalArgumentException {
if (s == null || "".equals(s)) {
setValue(null);
else {
// tokenize it with ","
String[] tokens = StringUtils.commaDelimitedListToStringArray(s);
RuleBasedTransactionAttribute attr = new RuleBasedTransactionAttribute();
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].trim();
if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_PROPAGATION)) {
attr.setPropagationBehaviorName(token);
else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ISOLATION)) {
attr.setIsolationLevelName(token);
else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_TIMEOUT)) {
String value = token.substring(DefaultTransactionAttribute.PREFIX_TIMEOUT.length());
attr.setTimeout(Integer.parseInt(value));
else if (token.equals(RuleBasedTransactionAttribute.READ_ONLY_MARKER)) {
attr.setReadOnly(true);
else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_COMMIT_RULE)) {
attr.getRollbackRules().add(new NoRollbackRuleAttribute(token.substring(1)));
else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ROLLBACK_RULE)) {
attr.getRollbackRules().add(new RollbackRuleAttribute(token.substring(1)));
else {
throw new IllegalArgumentException("Illegal transaction attribute token: [" + token + "]");
setValue(attr);
Java代码
/**  * PropertyEditor for TransactionAttribute objects. Takes Strings of form  * <p><code>PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2</code>  * <p>where only propagation code is required. For example:  * <p><code>PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>  *  * <p>The tokens can be in <strong>any</strong> order. Propagation and isolation codes  * must use the names of the constants in the TransactionDefinition class. Timeout values  * are in seconds. If no timeout is specified, the transaction manager will apply a default  * timeout specific to the particular transaction manager.  *  * <p>A "+" before an exception name substring indicates that  * transactions should commit even if this exception is thrown;  * a "-" that they should roll back.  *  * @author Rod Johnson  * @author Juergen Hoeller  * @since 24.04.2003  * @see org.springframework.transaction.TransactionDefinition  * @see org.springframework.core.Constants  */ public class TransactionAttributeEditor extends PropertyEditorSupport {     /**      * Format is PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2.      * Null or the empty string means that the method is non transactional.      * @see java.beans.PropertyEditor#setAsText(java.lang.String)      */     public void setAsText(String s) throws IllegalArgumentException {         if (s == null || "".equals(s)) {             setValue(null);         }         else {                // tokenize it with ","             String[] tokens = StringUtils.commaDelimitedListToStringArray(s);             RuleBasedTransactionAttribute attr = new RuleBasedTransactionAttribute();             for (int i = 0; i < tokens.length; i++) {                 String token = tokens[i].trim();                 if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_PROPAGATION)) {                     attr.setPropagationBehaviorName(token);                 }                 else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ISOLATION)) {                     attr.setIsolationLevelName(token);                 }                 else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_TIMEOUT)) {                     String value = token.substring(DefaultTransactionAttribute.PREFIX_TIMEOUT.length());                     attr.setTimeout(Integer.parseInt(value));                 }                 else if (token.equals(RuleBasedTransactionAttribute.READ_ONLY_MARKER)) {                     attr.setReadOnly(true);                 }                 else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_COMMIT_RULE)) {                     attr.getRollbackRules().add(new NoRollbackRuleAttribute(token.substring(1)));                 }                 else if (token.startsWith(RuleBasedTransactionAttribute.PREFIX_ROLLBACK_RULE)) {                     attr.getRollbackRules().add(new RollbackRuleAttribute(token.substring(1)));                 }                 else {                     throw new IllegalArgumentException("Illegal transaction attribute token: [" + token + "]");                 }             }             setValue(attr);         }     } }
从上面可以看出  token.substring() 这个方法把前缀timeout_给去掉了。。所以只剩下11了
从源码可看出来,PREFIX_XXXX  大都是这样写的,但前缀却是写在后面的。。觉得是不是命名有点古怪了,应该是XXXX_PREFIX,害我分析了一段时间。不过在源码上面的解释倒很清 楚:
* PropertyEditor for TransactionAttribute objects. Takes Strings of form  * <p><code>PROPAGATION_NAME,ISOLATION_NAME,readOnly,timeout_NNNN,+Exception1,-Exception2</code>  * <p>where only propagation code is required. For example:  * <p><code>PROPAGATION_MANDATORY,ISOLATION_DEFAULT</code>
文笔不好。。就给出个实例,我想一看就该明白了。
Xml代码
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!--  事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<!--  下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,timeout_11</prop>
</props>
</property>
</bean>
Xml代码
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<!--  事务拦截器bean需要依赖注入一个事务管理器 -->
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<!--  下面定义事务传播属性-->
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,timeout_11</prop>
</props>
</property>
</bean>
Xml代码
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">         <!--  事务拦截器bean需要依赖注入一个事务管理器 -->         <property name="transactionManager" ref="transactionManager"/>         <property name="transactionAttributes">             <!--  下面定义事务传播属性-->             <props>                 <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>                 <prop key="*">PROPAGATION_REQUIRED,timeout_11</prop>             </props>         </property>     </bean>
总结先到这里。。等深入研究的时候再把写详细些。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值