项目相关信息:该项目所用技术有Spring+SpringMvc+Mybatis,是一个Java web 项目,在Spring配置了数据库事务(注解式),项目团队的风格是将事务注解加在实现类上(我一般是加在接口上),按理说,加上了事务注解,当该service类中方法如果出现了异常,数据将会回滚至最初的状态,可是呢?按理说的状态并没有出现,出现的结果就和没有加事务的一模一样…………很是费解(所有配置文件写的都没问题)
最终呢,终于找到了一个看似不错的说法哦,说法如下:
由于采用的是SpringMvc、mybatis,故统一采用了标注来声明Service、Controller,由于服务器启动时的加载配置文件的顺序为web.xml——> root- context.xml(Spring的配置文件eg:applicationContext.xml)—— servlet-context.xml(SpringMvc的配置文件),由于root-context.xml配置文件中Controller会先进行扫描装配,但是此时的service还没有进行事务增强处理,得到的僵尸原样的service(没有经过事务加强处理,故而没有事务处理能力),所以我们必须在root-context.xml中不扫描Controller
Spring容器优先加载ServiceContextListener(对应applicationContext.xml)产生的父容器,而SpringMvc(对应mvc_dispatcher_servlet.xml)产生的是子容器。子容器Context进行扫描装配时装配的@Service注解的实例是没有经过事务加强处理,即没有事务处理能力的Service,而父容器进行初始化的Service是保证事务的增强处理能力的。如果不在子容器中将Service exclude掉,此时得到的将会是原样的无事务处理能力的Service,因为在多上下文的情况下,如果同一个bean被定义两次,后面一个优先。
(声明式情况)
修改Spring-mvc.xml
<!-- 自动扫描controller包下的所有类,如果@Controller注入为bean -->
<context:component-scan base-package="com.sds">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
Spring-common.xml(applicationContext.xml)
<!--自动扫描含有@Service将其注入为bean -->
<context:component-scan base-package="com.sds">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
spring-mvc.xml
<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />http://write.blog.csdn.net/postedit/77851040
</bean>
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="delete*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
</tx:attributes>
</tx:advice>
<!-- Spring aop事务管理 -->
<aop:config>
<aop:pointcut id="transactionPointcut"
expression="execution(* com.sds..*service.*.*(..))" />
<aop:advisor pointcut-ref="transactionPointcut"
advice-ref="transactionAdvice" />
</aop:config>
2017-9-14 再次更新:
之所以再次更新,是因为,找到了真正的原因所在,当然,上面的或许可以作为以后的参考:
首先我们了解一下spring 的事务机制:
默认spring事务只发生在未被捕捉RuntimeException时回滚。
Spring aop异常捕获原理:被拦截的方法需要显式抛出异常,不能经过处理,这样aop代理才能捕获到方法的异常,才能进行回滚。默认情况下aop只能捕获RuntimeException的异常,但可以通过配置来捕获特定的异常并回滚。换句话说,在Service的方法中不使用try-catch或者在catch中最后加上throw new RuntimeException(),这样程序发生异常时才会被aop捕获而回滚。
解决方案:
(1)例如Service层处理事务,纳闷Service中的方法中不做异常捕获,捉着在catch语句中最后增加throw new RUntimeException()语句,以便aop捕获异常再去回滚,并且在Service上层继续捕获这个异常并处理
(2)在Service层方法的catch语句中增加:TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();语句,手动回滚,这样上层就无需去处理异常。