详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt123

首先先看配置文件:

[html] view plaincopy

  1. <!-- hibernate -->

  2. <bean id="sessionFactory"

  3. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  4. <property name="dataSource" ref="dataSource" />

  5. <property name="hibernateProperties">

  6. <props>

  7. <prop key="hibernate.hbm2ddl.auto">update</prop>

  8. <prop key="hibernate.show_sql">true</prop>

  9. <prop key="hibernate.format_sql">true</prop>

  10. <prop key="connection.autoReconnect">true</prop>

  11. <prop key="connection.autoReconnectForPools">true</prop>

  12. <prop key="connection.is-connection-validation-required">true</prop>

  13. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

  14. </props>

  15. </property>

  16. <property name="mappingDirectoryLocations">

  17. <list>

  18. <value>classpath*:*oddtech/bean</value>

  19. </list>

  20. </property>

  21. </bean>

  22. <!-- 事務管理 -->

  23. <bean id="txManager"

  24. class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  25. <property name="sessionFactory" ref="sessionFactory" />

  26. </bean>

  27. <!-- 註解式事務的支持 -->

  28. <tx:annotation-driven transaction-manager="txManager"  order="0"/>

  29. <!-- 服務事務註冊切面 -->

  30. <aop:config >

  31. <aop:pointcut expression="execution(* oddtech.service.impl.*.*(..))"

  32. id="txPoint"  />

  33. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"  order="1"/>

  34. </aop:config>

  35. <tx:advice transaction-manager="txManager" id="txAdvice">

  36. <tx:attributes >

  37. <tx:method name="find*" propagation="REQUIRED" read-only="true"

  38. rollback-for="Exception" />

  39. <tx:method name="select*" propagation="REQUIRED" read-only="true"

  40. rollback-for="Exception" />

  41. <tx:method name="insert*" propagation="REQUIRED"

  42. rollback-for="Exception" />

  43. <tx:method name="delete*" propagation="REQUIRED"

  44. rollback-for="Exception" />

  45. <tx:method name="update*" propagation="REQUIRED"

  46. rollback-for="Exception" />

  47. <tx:method name="modify*" propagation="REQUIRED"

  48. rollback-for="Exception" />

  49. <tx:method name="*" read-only="true"

  50. rollback-for="Exception" />

  51. </tx:attributes>

  52. </tx:advice>

当在oddtech.service.impl包下某个类(Test)某个方法(insert)使用了@Transactional,相当于在执行new Test().insert() 方法执行2次AOP切面。那么我们需要通过order 属性去定义AOP切面的先后执行顺序。 order越小,在AOP的chain 中越靠前,越先执行。(chain模式)

所以 我们需要在<tx:annotation-driven transaction-manager="txManager"  order="0"/>中加入order属性为0,<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"  order="1"/>加入order 属性为1.那么他们两个的执行顺序如下:

。这样就属于事务嵌套

那么我们再看上边说的例子:当在oddtech.service.impl包下某个类(Test)某个方法(insert)使用了@Transactional,那么我们想让insert 方法只读,read-only=true,那么我们需要这样去定义:@Transactional(readOnly = true,propagation=Propagation.REQUIRED),为什么呢?

在声明事务中,我们对insert 的事务传播级别定义为:REQUIRED,关于事务传播级别,请参照http://blog.csdn.net/feng27156/article/details/8534609,那么在注解事务中我们定义REQUIRED 的事务。声明事务在使用注解定义的事务级别。

除非在特殊的情况下,注解事务不要和声明事务有冲突。声明事务定义的是统一的规则,如果你想让某一个方法有特殊的事务传播机制的话,那么不要和统一的规则有冲突。

<tx:method name="*" read-only="true" rollback-for="Exception" />

按照規則,定義一個test方法,加入:@Transactional定義。则test 方法爲read-only=false,propagation=REQUIRED。這是默認的。統一規則<tx:method name="*" read-only="true" rollback-for="Exception" />不會對test方法的註解事務衝突。

spring配置和注解事务同时存在导致的事务嵌套的更多相关文章

  1. spring boot:方法中使用try...catch导致@Transactional事务无效的解决(spring boot 2.3.4)

    一,方法中使用try...catch导致@Transactional事务无效的解决方法 1,问题的描述: 如果一个方法添加了@Transactional注解声明事务, 而方法内又使用了try catc ...

  2. spring配置redis注解缓存

    前几天在spring整合Redis的时候使用了手动的方式,也就是可以手动的向redis添加缓存与清除缓存,参考:http://www.cnblogs.com/qlqwjy/p/8562703.html ...

  3. spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)

    1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...

  4. Spring编程式注解使用不当导致其他事务无法正常提交

    1.事故背景 原本在使用的是注解式事务,后面因为需要在事务中增加异步推送机制,所以需要将推送机制放到事务之外,修改后发现系统经常出现事务长时间无法提交导致回滚. 2.排查流程 (1)一开始重启应用是能 ...

  5. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

    转: http://blog.csdn.net/it_man/article/details/5074371 Spring事务配置的五种方式 前段时间对Spring的事务配置做了比较深入的研究,在此之 ...

  6. SpringMVC对包的扫描范围扩大后,导致的事务配置不生效问题

    问题场景 项目使用的框架:Spring 4.1.4 + Hibernate 4.3.8 + MySQL. web.xml中对Spring的配置: <!-- 把 Spring 容器集成到 Web ...

  7. Spring笔记04_AOP注解开发_模板_事务

    目录 1. Spring基于AspectJ的注解的AOP开发 1. 1 SpringAOP的注解入门 1.2 Spring的AOP的注解通知类型 1.2.1 @Before:前置通知 1.2.2 @A ...

  8. spring boot整合shiro后,部分注解(Cache缓存、Transaction事务等)失效的问题

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/elonpage/article/details/78965176 前言 整合有缓存.事务的sprin ...

  9. Spring配置--tx事务配置方式

    前段时间对Spring的事务配置做了比较深入的研究,在此之间对Spring的事务配置虽说也配置过,但是一直没有一个清楚的认识.通过这次的学习发觉Spring的事务配置只要把思路理清,还是比较好掌握的. ...

随机推荐

  1. C++ STL vector详解

    一.解释:  vector(向量):是一种顺序容器,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界.而vector正好弥补了这个缺陷,它 ...

  2. 《奇思妙想》在JavaScript语言中floor和round方法在某种随机分配场景下对分配区间的公平性!!!

    前言 大欢哥的题目完成了,但是衍生出一个新的问题!上篇随笔中我和大欢哥采用的随机数生成方式,到底是谁的比较公平??? 正文 欢迎来到阿段博客<奇思妙想>!我们的口号是 “心有多大,bug就 ...

  3. mybatis example使用 and和or联合查询(转)

    这两天项目用到ibatis,碰到and or的联合查询,语句像这样的 select * from table where xxx = "xxx" and (xx1="xx ...

  4. 自定义Git

    在安装Git一节中,我们已经配置了user.name和user.email,实际上,Git还有很多可配置项. 比如,让Git显示颜色,会让命令输出看起来更醒目: $ git config --glob ...

  5. 深度学习:Keras入门(二)之卷积神经网络(CNN)

    说明:这篇文章需要有一些相关的基础知识,否则看起来可能比较吃力. 1.卷积与神经元 1.1 什么是卷积? 简单来说,卷积(或内积)就是一种先把对应位置相乘然后再把结果相加的运算.(具体含义或者数学公式 ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 在django中集成ckeditor富文本

    目前用的比较多的富文本插件有百度的ueditor.ckeditor.kindeditor等,其中ueditor和kindeditor比较美观,ckeditor的皮肤较少.但是ueditor加载较慢,k ...

  8. 关于isNAN()函数内进行的莫名其妙的隐式转换?!

    isNaN(NaN); // true isNaN(undefined); // true isNaN({}); // true isNaN(true); // false isNaN(null); ...

  9. Oracle execute and call

    --execute和call的区别 -------------------------2014/01/14 EXEC is a sqlplus command that put its argumen ...

  10. leetcode range sum query

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...