spring mvc事务没有生效的原因

129 篇文章 8 订阅
24 篇文章 0 订阅

spring-mvc事务配置如下

 

<tx:advice id="transactionAdvice" transaction-manager="transactionManager">

<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="append*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="edit*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="repair" propagation="REQUIRED" />
<tx:method name="withdrawals" propagation="REQUIRED" />
<tx:method name="pay" propagation="REQUIRED" />
<tx:method name="get*" propagation="SUPPORTS" />
<tx:method name="find*" propagation="SUPPORTS" />
<tx:method name="select*" propagation="SUPPORTS" />

<tx:method name="*" rollback-for="Exception" />
</tx:attributes>
</tx:advice>

<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* com.*.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />

</aop:config> 

 

但在service允许代码报错后,事务回滚不生效

根据 百度上的一些情况也总结了几种

第一种:在针对事务的类中抛出RuntimeException异常,而不是抛出Exception。

第二种: 不能在方法中使用try catch抛出异常,不然不会回滚

第三种:

  1. mysql默认存储引擎为MyISAM是不支持事务的,  
  2. 需要设置为InnoDB模式,通过show engines; 命令看到  

 

第4种:上面3种使用后都没有效果,百度到第4种方式

 

  1.  1.root-context.xml   
  2. <!-- 不扫描带有@Controller注解的类。因为这些类已经随容器启动时,在servlet-context中扫描过一遍了 -->   
  3. <context:component-scan base-package="com.kimho">   
  4. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>   
  5. </context:component-scan>   
  6.   
  7. 2、servlet-context.xml:   
  8. <!-- 扫描业务组件,让spring不扫描带有@Service注解的类(留在root-context.xml中扫描@Service注解的类),防止事务失效 -->   
  9. <context:component-scan base-package="com.kimho">   
  10. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>   
  11. </context:component-scan>   

将在spring-mvc.xml下的

 

<context:component-scan base-package="com.aa.*" >
</context:component-scan>

改成

<context:component-scan base-package="com.aa.*" >
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>

进行扫描 既可。

原因是:如果使用了spring+mvc,则context:component-scan重复扫描问题可能会引起事务失败,因为

spring的子容器先于父容器启动,造成在controller中注入service时还没有加载事务;

有2种配置方法防止重复扫描:

 

1 在主容器中(applicationContext.xml),将Controller的注解排除掉:

 

<context:component-scan base-package="com">
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>


 2 或者在springMVC配置文件中将Service注解排除掉 :

 

 

<context:component-scan base-package="com">
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan> 

 

 

 

因为spring的context是父子容器,所以会产生冲突,Controller会首先被扫描装配,而此时的Service还没有进行事务的加强处理(AOP),获得的将是原样的Service(没有经过事务加强处理,故而没有事务处理功能) ,最后才是applicationContext.xml中的扫描进行事务处理。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值