springmvc mybatis 事务管理不生效原因
2017-02-17 16:50
453 查看
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抛出异常,不然不会回滚
第三种:
mysql默认存储引擎为MyISAM是不支持事务的,
需要设置为InnoDB模式,通过show engines; 命令看到
第4种:上面3种适用后都没有效果百度到第4种方式
1.root-context.xml
<!-- 不扫描带有@Controller注解的类。因为这些类已经随容器启动时,在servlet-context中扫描过一遍了 -->
<context:component-scan base-package="com.kimho">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2、servlet-context.xml:
<!-- 扫描业务组件,让spring不扫描带有@Service注解的类(留在root-context.xml中扫描@Service注解的类),防止事务失效 -->
<context:component-scan base-package="com.kimho">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</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>
进行扫描 既可
<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抛出异常,不然不会回滚
第三种:
mysql默认存储引擎为MyISAM是不支持事务的,
需要设置为InnoDB模式,通过show engines; 命令看到
第4种:上面3种适用后都没有效果百度到第4种方式
1.root-context.xml
<!-- 不扫描带有@Controller注解的类。因为这些类已经随容器启动时,在servlet-context中扫描过一遍了 -->
<context:component-scan base-package="com.kimho">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
2、servlet-context.xml:
<!-- 扫描业务组件,让spring不扫描带有@Service注解的类(留在root-context.xml中扫描@Service注解的类),防止事务失效 -->
<context:component-scan base-package="com.kimho">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</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整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring+mybatis使用事务管理不生效原因分析
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- spring整合 mybatis 事务管理不生效原因
- springMVC mybatis mysql 配置 事务不起作用的原因
- springmvc mybatis 声明式事务管理回滚失效,(checked回滚)捕捉异常,传输错误信息
- springmvc mybatis 多数据源 事务不生效
- springMVC mybatis 事务管理
- springmvc mybatis 声明式事务管理回滚失效,(checked回滚)捕捉异常,传输错误信息
- SpringMVC + Spring + MyBatis 学习笔记:SpringMVC和Spring一同工作的时候,AOP事务管理不起作用的解决方法