spring+ mybatis 事务不能回滚问题解决
2017-04-25 13:52
656 查看
最近遇到一个回滚问题,百度了一天了,终于解决了;
百度上遇到很多中情况,我记录的情况主要是以下几点(1)错误类型是Exception,加rollback-for="excepation"就可以解决,使用try()catch(){}捕获异常你确没有把他重新抛出去,直接去掉try cathc就行(2)如果是使用aop不是注解管理事务,可能是<aop:pointcut id="confService" expression="execution(* com.cn.hnust.service.impl.*.*(..))
"/>写错(3)扫描的时候把controller和service层一起扫了,造成事务还没有配置你就开始装配了 。
我的错误类型是第三种。解决方式如下:
我的事务管理用的是aop我贴出代码:
一、首先确保你这个路径是对的
二、在applicationControlxml(spring的配置文件)
确保有这个并且最重要的是base-pakepage="com.cn.hnust.service"
不要只写base-pakepag="com",要精确到你那个要回滚的事务,一般是在service中,就是精确搭配service
三、在springmvc-servlet.xml(springMVC配置文件)
同样情况base-pakeage="com.cn.hnust.controller"
不能只写base-package="com.cn.hnust" 要精确到controller
主要的原理是因为:spring先加载springMVC-servlet.xml文件,如果只写base-package="com.cn.hnust也会把hnust下的service和controller同时装配进去,然而回滚是要先要扫描controller层,然后在service层,有一个先后顺序,所以在springMVC-servlet.xml中base-package后面的值要写到com.cn.hnust.controlle层,不要只写到com.cn.hnust;否则会把com.cn.hnus下的service和controller层同时扫进去,这样就造成事务管理都没有配置,你就开始装配了。
百度上遇到很多中情况,我记录的情况主要是以下几点(1)错误类型是Exception,加rollback-for="excepation"就可以解决,使用try()catch(){}捕获异常你确没有把他重新抛出去,直接去掉try cathc就行(2)如果是使用aop不是注解管理事务,可能是<aop:pointcut id="confService" expression="execution(* com.cn.hnust.service.impl.*.*(..))
"/>写错(3)扫描的时候把controller和service层一起扫了,造成事务还没有配置你就开始装配了 。
我的错误类型是第三种。解决方式如下:
我的事务管理用的是aop我贴出代码:
<tx:advice id="TestAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception.class"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="confService" expression="execution(* com.cn.hnust.service.impl.*.*(..)) "/> <aop:advisor advice-ref="TestAdvice" pointcut-ref="confService"></aop:advisor> </aop:config> <aop:aspectj-autoproxy proxy-target-class="true"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean>
一、首先确保你这个路径是对的
<aop:pointcut id="confService" expression="execution(* com.cn.hnust.service.impl.*.*(..)) "/>
二、在applicationControlxml(spring的配置文件)
<context:component-scan base-package="com.cn.hnust.service"></context:component-scan>
确保有这个并且最重要的是base-pakepage="com.cn.hnust.service"
不要只写base-pakepag="com",要精确到你那个要回滚的事务,一般是在service中,就是精确搭配service
三、在springmvc-servlet.xml(springMVC配置文件)
<context:component-scan base-package="com.cn.hnust.controller"></context:component-scan>
同样情况base-pakeage="com.cn.hnust.controller"
不能只写base-package="com.cn.hnust" 要精确到controller
主要的原理是因为:spring先加载springMVC-servlet.xml文件,如果只写base-package="com.cn.hnust也会把hnust下的service和controller同时装配进去,然而回滚是要先要扫描controller层,然后在service层,有一个先后顺序,所以在springMVC-servlet.xml中base-package后面的值要写到com.cn.hnust.controlle层,不要只写到com.cn.hnust;否则会把com.cn.hnus下的service和controller层同时扫进去,这样就造成事务管理都没有配置,你就开始装配了。
相关文章推荐
- 终极办法:解决spring mvc+mysql+mybatis事务不提交不回滚的问题
- mybatis整合spring、springmvc时业务层无法进行事务回滚问题解决
- mybatis-spring配置正常事务却无法回滚(建表问题)
- Spring中解决Aop 事务嵌套回滚问题
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
- Mybatis+SpringMVC+注解事务遇到的一些问题及解决
- 解决Spring的声明式事务中的自定义异常不回滚问题
- 实践中遇到Ibatis和spring整合事务不回滚的问题解决
- 在Mybatis-spring中由于默认Autowired导致不能配置多个数据源的问题分析及解决
- Proxool+hibernate+spring时事务自动回滚导致无法提交的问题解决
- 在Mybatis-spring中由于默认Autowired导致不能配置多个数据源的问题分析及解决
- spring-mybatis 事务回滚问题
- springboot+mybatis解决多数据源切换事务控制不生效的问题
- Spring 事务不回滚的问题
- Spring中Transactional不添加rollbackFor等属性时的事务回滚问题
- c3p0连接池事务不能回滚的问题
- Biztalk开发中解决“新事务不能登记到指定的事务处理器中”问题 .
- spring的事务回滚问题
- springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题
- JPA与Spring2.5整合时发生不能创建entityManagerFactory的问题解决方法