Spring配置事务
来源:互联网 发布:win10网络发现已关闭 编辑:程序博客网 时间:2024/04/28 04:10
一、
采用的基本搭建环境: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,配置如下:
Java代码
- <!-- 自动扫描组件,这里要把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,配置如下:
Java代码
- <!-- 扫描所有的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>
下面就可以进行配置声明式事务管理了,配置如下:
Java代码
- <!-- 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声明式事务管理配置完成,并运行正确
二、
先来看看这个spring的配置文件的配置:
<!-- 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务的传播特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置事务拦截器拦截哪些类的哪些方法,一般设置成拦截Service -->
<aop:config>
<aop:pointcut expression="execution(* com.xy.service.*.*(..))"
id="allDaoMethod" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allDaoMethod" />
</aop:config>
表示com.xy.service包下的所有方法为为事务管理。
execution(* com.aptech.jb.epet.dao.hibimpl.*.*(..))
这样写应该就可以了 这是com.aptech.jb.epet.dao.hibimpl 包下所有的类的所有方法。。
第一个 *(星)代表所有的返回值类型
第二个 *(星)代表所有的类
第三个 *(星)代表类所有方法 最后一个 ..(两个点)代表所有的参数。
注意: .. 表示任意路径
下面给出一些常见切入点表达式的例子:
- 任意公共方法的执行:
execution(public * *(..))
- 任何一个以“set”开始的方法的执行:
execution(* set*(..))
- AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
- 定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
- 定义在service包或者子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
0 0
- spring 事务配置
- Spring+Hibernate配置事务
- Spring 事务简化配置
- Spring AOP事务配置
- Spring 事务简化配置
- spring hibernate 事务配置
- Spring配置事务
- spring 事务配置方法
- spring 事务配置
- 关于Spring 事务配置
- Spring事务配置详解
- Spring 事务配置
- Spring 事务配置
- Spring的事务配置
- java spring 事务配置
- Spring事务配置详解
- Spring事务配置
- spring声明事务配置
- windows内核下字符数组转Unicode
- nodejs+express+mongodb简单的例子
- 三种主流的Web服务实现方案(REST+SOAP+XML-RPC)简述及比较
- 新浪微博第三方登陆
- 在struts2中重定向加参数
- Spring配置事务
- TCP拥塞控制
- 斗地主算法逻辑中的天之道
- Fmincon in Matlab 2014a
- 遇到在html中取值时,值为null的问题
- Linux下无法清空回收站
- 用户界面控件套包ComponentOne Studio Enterprise 2015 v3全新发布
- 纸上情,唯爱
- asp.net页面的提交方式POST和GET区别及用法?