欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringMVC+MyBatis声明式事务管理

程序员文章站 2024-03-05 13:56:18
采用的基本搭建环境:springmvc、mybatis、mysql、tomcat        ...
采用的基本搭建环境:springmvc、mybatis、mysql、tomcat
        spring事务管理分解了传统的全局事务管理和本地事务管理的劣势,使得在任何环境中都可以使用统一的事务管理模型,你可以写一次代码,然后在不同的环境从你的代码里面配置不同的事务管理策略,spring提供两种事务管理策略:一种是声明式事务管理策略,另一种是编程式事务管理策略,这里主要介绍声明式事务管理策略
由于采用的是springmvc、 mybatis,故统一采用了标注来声明service、controller
由于服务器启动时的加载配置文件的顺序为web.xml---root-context.xml(spring的配置文件)---servlet-context.xml(springmvc的配置文件),由于root-context.xml配置文件中controller会先进行扫描装配,但是此时service还没有进行事务增强处理,得到的将是原样的service(没有经过事务加强处理,故而没有事务处理能力),所以我们必须在root-context.xml中不扫描controller,配置如下:
<!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。  --> 
 <context:component-scan base-package="com.sence"> 
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller" />  
 .</context:component-scan> 
 <!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。  -->
 <context:component-scan base-package="com.sence">
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.controller" /> 
 </context:component-scan>
在servlet-context.xml中扫描controller同时不扫描service,配置如下:
<!-- 扫描所有的controller 但是不扫描service--> 
 <context:component-scan base-package="com.sence"> 
 <context:include-filter type="annotation" expression="org.springframework.stereotype.controller" /> 
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.service" /> 
 </context:component-scan> 
 <!-- 扫描所有的controller 但是不扫描service-->
 <context:component-scan base-package="com.sence">
 <context:include-filter type="annotation" expression="org.springframework.stereotype.controller" />
 <context:exclude-filter type="annotation" expression="org.springframework.stereotype.service" />
 </context:component-scan>
下面就可以进行配置声明式事务管理了,配置如下:
<!-- transaction manager, use datasourcetransactionmanager --> 
 <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> 
 <property name="datasource" ref="datasource" /> 
 </bean> 
 <!-- spring declarative transaction management --> 
 <aop:config> 
 <aop:pointcut id="fooservicemethods"  
 expression="execution(* com.sence.*.service.impl.*.*(..))"/>  
 <aop:advisor advice-ref="txadvice" pointcut-ref="fooservicemethods"/>  
 </aop:config> 
 <tx:advice id="txadvice" transaction-manager="txmanager"> 
 <tx:attributes> 
 <tx:method name="find*" read-only="true"/> 
 <tx:method name="load*" read-only="true"/> 
 <tx:method name="*" rollback-for="customexception"/> 
 </tx:attributes> 
 </tx:advice> 
 <!-- transaction manager, use datasourcetransactionmanager -->
 <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
 <property name="datasource" ref="datasource" />
 </bean>
 <!-- spring declarative transaction management -->
<aop:config>
 <aop:pointcut id="fooservicemethods" 
 expression="execution(* com.sence.*.service.impl.*.*(..))"/> 
 <aop:advisor advice-ref="txadvice" pointcut-ref="fooservicemethods"/> 
 </aop:config>
 <tx:advice id="txadvice" transaction-manager="txmanager">
<tx:attributes>
 <tx:method name="find*" read-only="true"/>
 <tx:method name="load*" read-only="true"/>
 <tx:method name="*" rollback-for="customexception"/>
 </tx:attributes>
 </tx:advice>
到此我的配置完成了,但是经过我的测试,当我往mysql数据库表批量增加对象时,当其中一个对象出现错误,抛出customexception事务却不回滚,这个真是令人头疼,于是我继续查找,步骤如下:
1. 查找是否声明式事务管理有误,如切入点写错了
2. 查找controller扫描部分配置是否正确
但是这两点我都查了,还是事务没有回滚,这个时候我没办法了,只能动用终极武器了:查看源码,开始debug程序,发现进入到了事务,并且出现了异常,捕获后进入到了回滚程序,但是数据库却没有回滚,为了避免spring自己的abstractplatformtransactionmanager的干扰,我自己定制了一个事务管理类并继承配置文件中的datasourcetransactionmanager类,这样可以清楚的看到程序的运行轨迹,继续debug,还是出现了异常,捕获后进入到了回滚程序,但是数据库却没有回滚,此刻我开始怀疑mysql数据库的事务支持功能了,于是网上查找mysql对事务的支持,发现mysql4.0以后可以支持事务,但是mysql的数据表分为两类,一类是传统的数据表,另一类则是支持事务的数据表。支持事务的数据表分为两种:innodb和berkeleydb
使用一下命令:show create table ***  查看我的数据库表的属性才发现我的表原来是传统类型的表,于是我使用navicat更改了表的类型为:innodb,然后运行程序发现事务回滚了
到此springmvc声明式事务管理配置完成,并运行正确