tx标签配置事务却无法回滚

来源:互联网 发布:免费爆吧软件 编辑:程序博客网 时间:2024/05/15 16:18

xml配置如下:

<!-- 属性文件读入 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath*:config/jdbc.properties</value></list></property></bean><!-- 支持 @Transactional 标记 --><tx:annotation-driven/><!-- 支持 @AspectJ 标记--><aop:aspectj-autoproxy/><!-- 以AspectJ方式 定义 AOP --><aop:config proxy-target-class="true"><aop:advisor pointcut="execution(* org.springside.core.dao.*Dao.*(..))" advice-ref="txAdvice"/></aop:config><!-- 基本事务定义,使用transactionManager作事务管理,默认get*方法的事务为readonly,其余方法按默认设置. 默认的设置请参考Spring文档事务一章. --><tx:advice id="txAdvice" ><tx:attributes>   <tx:method name="query*" read-only="true"   propagation="NOT_SUPPORTED"/><tx:method name="get*" read-only="true"   propagation="NOT_SUPPORTED"/><tx:method name="save*" read-only="true"  propagation="REQUIRED"/></tx:attributes></tx:advice>
有时我们发现save方法插入数据库出现异常时并不会回滚,因为aop默认只捕获RunTimeException,如果要捕获异常并回滚,可以加上属性

rollback-for:"java.lang.Exception"

其他相关属性如下

属性 是否需要? 默认值 描述name是 

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

propagation不REQUIRED事务传播行为isolation不DEFAULT事务隔离级别timeout不-1事务超时的时间(以秒为单位)read-only不false事务是否只读?rollback-for不 

将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'

no-rollback-for不 

 被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException


0 0
原创粉丝点击