默认的 <tx:advice/> 设置如下:

事务传播设置是 REQUIRED隔离级别是 DEFAULT事务是 读/写事务超时默认是依赖于事务系统的,或者事务超时没有被支持。任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚

这些默认的设置当然也是可以被改变的。 <tx:advice/> 和 <tx:attributes/> 标签里的 <tx:method/> 各种属性设置总结如下:rollback-for/no-rollback-for:配置哪些异常可以导致/不导致回滚,在默认情况下,抛出RuntimeException或其子类将导致回滚,其它异常不会导致回滚 .

tx:method

属性

类型

默认值

说明

name

 

与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*' 、'handle*' 、'on*Event' 等等。

propagation

Propagation枚举 REQUIRED 事务传播属性
isolation isolation枚举 DEFAULT(所用数据库默认级别) 事务隔离级别
read-only boolean false 是否才用优化的只读事务
timeout int -1 超时(秒)
rollbackFor Class[] {} 需要回滚的异常类
noRollbackFor Class[] {} 不需要回滚的异常类

Spring中事物的传播级别:

PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。

解惑 spring 嵌套事务 (转载自http://www.javaeye.com/topic/35907?page=1)

/**
* @date 2006-11-24
* @note 转载自http://www.javaeye.com/topic/35907?page=1
*/
********TransactionDefinition 接口定义*******************
/** * Support a current transaction, create a new one if none exists. * Analogous to EJB transaction attribute of the same name. *
This is typically the default setting of a transaction definition. */ int PROPAGATION_REQUIRED = 0; /** * Support a current transaction, execute non-transactionally if none exists. * Analogous to EJB transaction attribute of the same name. *
Note: For transaction managers with transaction synchronization, * PROPAGATION_SUPPORTS is slightly different from no transaction at all, * as it defines a transaction scopp that synchronization will apply for. * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) * will be shared for the entire specified scope. Note that this depends on * the actual synchronization configuration of the transaction manager. * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization */ int PROPAGATION_SUPPORTS = 1; /** * Support a current transaction, throw an exception if none exists. * Analogous to EJB transaction attribute of the same name. */ int PROPAGATION_MANDATORY = 2; /** * Create a new transaction, suspend the current transaction if one exists. * Analogous to EJB transaction attribute of the same name. *
Note: Actual transaction suspension will not work on out-of-the-box * on all transaction managers. This in particular applies to JtaTransactionManager, * which requires the javax.transaction.TransactionManager to be * made available it to it (which is server-specific in standard J2EE). * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager */ int PROPAGATION_REQUIRES_NEW = 3; /** * Execute non-transactionally, suspend the current transaction if one exists. * Analogous to EJB transaction attribute of the same name. *
Note: Actual transaction suspension will not work on out-of-the-box * on all transaction managers. This in particular applies to JtaTransactionManager, * which requires the javax.transaction.TransactionManager to be * made available it to it (which is server-specific in standard J2EE). * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager */ int PROPAGATION_NOT_SUPPORTED = 4; /** * Execute non-transactionally, throw an exception if a transaction exists. * Analogous to EJB transaction attribute of the same name. */ int PROPAGATION_NEVER = 5; /** * Execute within a nested transaction if a current transaction exists, * behave like PROPAGATION_REQUIRED else. There is no analogous feature in 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 * DataSourceTransactionManager when working on a JDBC 3.0 driver. * Some JTA providers might support nested transactions as well. * @see org.springframework.jdbc.datasource.DataSourceTransactionManager */ int PROPAGATION_NESTED = 6; 

在这篇文章里,他用两个嵌套的例子辅助分析,我这里直接引用了。

********************sample***********************
ServiceA { /** * 事务属性配置为 PROPAGATION_REQUIRED */ void methodA() { ServiceB.methodB(); }
} ServiceB {  /** * 事务属性配置为 PROPAGATION_REQUIRED */ void methodB() { }
}
*************************************************

1: PROPAGATION_REQUIRED

加入当前正要执行的事务不在另外一个事务里,那么就起一个新的事务
比如说,ServiceB.methodB的事务级别定义为PROPAGATION_REQUIRED, 那么由于执行ServiceA.methodA的时候,
ServiceA.methodA已经起了事务,这时调用ServiceB.methodB,ServiceB.methodB看到自己已经运行在ServiceA.methodA
的事务内部,就不再起新的事务。而假如ServiceA.methodA运行的时候发现自己没有在事务中,他就会为自己分配一个事务。
这样,在ServiceA.methodA或者在ServiceB.methodB内的任何地方出现异常,事务都会被回滚。即使ServiceB.methodB的事务已经被
提交,但是ServiceA.methodA在接下来fail要回滚,ServiceB.methodB也要回滚

2: PROPAGATION_SUPPORTS

如果当前在事务中,即以事务的形式运行,如果当前不再一个事务中,那么就以非事务的形式运行

3: PROPAGATION_MANDATORY

必须在一个事务中运行。也就是说,他只能被一个父事务调用。否则,他就要抛出异常 

4: PROPAGATION_REQUIRES_NEW

这个就比较绕口了。 比如我们设计ServiceA.methodA的事务级别为
PROPAGATION_REQUIRED,ServiceB.methodB的事务级别为PROPAGATION_REQUIRES_NEW,
那么当执行到ServiceB.methodB的时候,ServiceA.methodA所在的事务就会挂起,
ServiceB.methodB会起一个新的事务,等待ServiceB.methodB的事务完成以后,
他才继续执行。他与PROPAGATION_REQUIRED 的事务区别在于事务的回滚程度了。
因为ServiceB.methodB是新起一个事务,那么就是存在
两个不同的事务。如果ServiceB.methodB已经提交,那么ServiceA.methodA失败回滚,
ServiceB.methodB是不会回滚的。如果ServiceB.methodB失败回滚,
如果他抛出的异常被ServiceA.methodA捕获,ServiceA.methodA事务仍然可能提交。

5: PROPAGATION_NOT_SUPPORTED

当前不支持事务。比如ServiceA.methodA的事务级别是PROPAGATION_REQUIRED ,而ServiceB.methodB
的事务级别是PROPAGATION_NOT_SUPPORTED , 那么当执行到ServiceB.methodB时,
ServiceA.method的事务挂起,而他以非事务的状态运行完,再继续ServiceA.methodA的事务。 

6: PROPAGATION_NEVER

不能在事务中运行。假设ServiceA.methodA的事务级别是PROPAGATION_REQUIRED,
而ServiceB.methodB的事务级别是PROPAGATION_NEVER , 那么ServiceB.methodB
就要抛出异常了。

7: PROPAGATION_NESTED

理解Nested的关键是savepoint。他与PROPAGATION_REQUIRES_NEW的区别是,
PROPAGATION_REQUIRES_NEW另起一个事务,将会与他的父事务相互独立,
而Nested的事务和他的父事务是相依的,他的提交是要等和他的父事务一块提交的。也就是说,
如果父事务最后回滚,他也要回滚的。 而Nested事务的好处是他有一个savepoint。 
*****************************************
ServiceA { /**
* 事务属性配置为 PROPAGATION_REQUIRED
*/ void methodA() { try { //savepoint ServiceB.methodB(); //PROPAGATION_NESTED 级别 } catch (SomeException) { // 执行其他业务, 如 ServiceC.methodC(); } } }
********************************************

也就是说ServiceB.methodB失败回滚,那么ServiceA.methodA也会回滚到savepoint点上,ServiceA.methodA可以选择另外一个分支,比如 
ServiceC.methodC,继续执行,来尝试完成自己的事务。

spring事务管理tx:Advice详解相关推荐

  1. Spring事务管理-tx:advice标签

    首先先看一下代码: <bean name="transactionManager" class="org.springframework.jdbc.datasour ...

  2. Spring声明式事务管理的配置详解

    环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add ...

  3. spring事务注解@Transactional参数详解

    在Spring中进行事务管理操作有两种方式:编程式事务管理和声明式事务管理.这里介绍的是第二种方式. 1.声明式事务管理(底层使用AOP原理): 1>基于注解方式.(本文章基于注解) 2> ...

  4. Spring事务通知tx:advice标签

    spring配置文件样例 简单的事务配置,对save/delete开头的方法加事务,get/find开头的设置为不加事务只读模式 <tx:advice id="txAdvice&quo ...

  5. Spring事务管理(详解+实例)

    写这篇博客之前我首先读了<Spring in action>,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下: ...

  6. Spring进阶(五):Spring事务管理(详解+实例)

    文章目录 一.前言 二.核心接口 2.1 事务管理器 2.1.1 JDBC事务 2.1.2 Hibernate事务 2.1.3 Java持久化API事务(JPA) 2.1.4 Java原生API事务 ...

  7. 关于事务管理的理解和Spring事务管理详解

    转载于:http://www.mamicode.com/info-detail-1248286.html 1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000 ...

  8. Spring事务管理详解_基本原理_事务管理方式

    Spring事务管理详解_基本原理_事务管理方式 1. 事务的基本原理 Spring事务的本质其实就是数据库对事务的支持,使用JDBC的事务管理机制,就是利用java.sql.Connection对象 ...

  9. Spring事务管理详解

    什么是事务 事务是逻辑上的一组操作,要么都执行,要么都不执行. 需要注意的是:事务能否生效数据库引擎是否支持事务是关键.比如常用的 MySQL 数据库默认使用支持事务的 innodb引擎.但是,如果把 ...

最新文章

  1. ios10中禁止用户缩放页面
  2. Windows 2003 主域控和DNS迁移到Windows 2008 R2(1)
  3. 在Windows Mobile和Wince(Windows Embedded CE)下如何使用.NET Compact Framework开发进程管理程序...
  4. C#正则表达式提取HTML中IMG标签的SRC地址(转)
  5. 一位互联网老兵分享,社会化运营案例解析(宜信内部干货)
  6. 原理 快速邻近匹配_论文推荐 | 陈晓勇:低空摄影测量立体影像匹配的现状与展望...
  7. Web前端开发笔记——第三章 CSS语言 第七节 圆角边框、阴影
  8. Decision Tree(DT)决策树
  9. 漫画:趣解鸿蒙 OS 如何实现跨平台?
  10. 【优化算法】混沌博弈优化算法(CGO)【含Matlab源码 1803期】
  11. MATLAB | 老版本也能用,默认设置让简单的代码画出炫酷的图像
  12. html添加启动项,windows boot manager启动项是什么
  13. 日升日落,总有黎明——暖色系原木风装修
  14. 直观理解语义分割中IOU
  15. 协同(OA)应用中的七个机制
  16. 如何解决Tomcat下中文乱码问题?
  17. 《程序员的第一年》---------- 周未回想
  18. Spring3 MVC详解二
  19. hs8545m5虚拟服务器,华为HS8545M光猫怎么开启路由功能
  20. 第一弹!安排!安利10个让你爽到爆的IDEA必备插件!

热门文章

  1. Python3,2行代码添加水印,发朋友圈,图片再也不怕被盗了!!!
  2. 热烈欢迎中消云物联网研究院一行莅临上海铭控!
  3. CSS第三章:8.设置元素圆角、椭圆角效果
  4. 拉伯证券|A股大涨!外资30分钟爆买百亿!汽车股狂飙
  5. 笑话--老外与山东妞(爆笑)
  6. Android获取不到运动步数(踩坑)
  7. 现货黄金有什么需要注意的?
  8. 全家都是博士是一种什么样的体验?
  9. 中秋节后如何有面子的带女票回家?
  10. 直播预告|无监督学习的公平性研究——美国布兰迪斯大学助理教授刘洪甫