spring 注解 事务,声明事务共存的情况下,先后顺序 order

 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     <!-- 事務管理 -->
24     <bean id="txManager"
25         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
26         <property name="sessionFactory" ref="sessionFactory" />
27     </bean>
28     <!-- 註解式事務的支持 -->
29     <tx:annotation-driven transaction-manager="txManager"  order="0"/>
30     <!-- 服務事務註冊切面 -->
31     <aop:config >
32         <aop:pointcut expression="execution(* oddtech.service.impl.*.*(..))"
33             id="txPoint"  />
34         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"  order="1"/>
35     </aop:config>
36 
37     
38 
39 
40     <tx:advice transaction-manager="txManager" id="txAdvice">
41         <tx:attributes >
42             <tx:method name="find*" propagation="REQUIRED" read-only="true" 
43                 rollback-for="Exception" />
44             <tx:method name="select*" propagation="REQUIRED" read-only="true"
45                 rollback-for="Exception" />
46             <tx:method name="insert*" propagation="REQUIRED"  
47                 rollback-for="Exception" />
48             <tx:method name="delete*" propagation="REQUIRED"
49                 rollback-for="Exception" />
50             <tx:method name="update*" propagation="REQUIRED" 
51                 rollback-for="Exception" />
52             <tx:method name="modify*" propagation="REQUIRED"
53                 rollback-for="Exception" />
54             <tx:method name="*" read-only="true"
55                 rollback-for="Exception" />
56         </tx:attributes>
57     </tx:advice>

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

 

假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html

所以 我们需要在<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方法的註解事務衝突。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值