Spring 2.X的事务配置策略
虽然前面介绍的TransactionProxyFactoryBean配置策略简单易懂,但配置起来极为麻烦:每个目标Bean都需要配置一个TransactionProxyFactoryBean代理,这种方式将导致配置文件急剧增加。
Spring 2.X的XMLSchema方式提供了更简洁的事务配置策略,Spring 2.X提供了tx命名空间来配置事务管理,tx命名空间提供了tx:advice../元素来配置事务增强处理,一旦使用该元素配置了增强处理,就可以直接使用aop:advisor../元素启用自动代理了。
下面的应用示例依然使用前面(点击查看)提供的NewsDao接口和NewsDaoImpl实现类,但改为使用tx:advice../元素来配置事务增强处理,再使用tx:advisor../为容器中一批Bean配置自动事务代理。下面是配置文件:
1<?xml version="1.0" encoding="utf-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/aop/spring-tx-3.0.xsd 14 http://www.springframework.org/schema/context 15 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 16 17 <!-- 使用C3P0数据库连接池作为数据源 --> 18 <bean id="dataSource" 19 class="com.mchange.v2.c3p0.ComboPolledDataSource" destroy-method="close"> 20 <property name="driverClass" value="com.mysql.jdbc.Driver" /> 21 <property name="jdbcUrl" value="jdbc:mysql://localhost/test" /> 22 <property name="user" value="root" /> 23 <property name="password" value="root" /> 24 <property name="maxPoolSize" value="40" /> 25 <property name="minPoolSize" value="4" /> 26 <property name="initialPoolSize" value="10" /> 27 <!-- 指定数据库连接池的连接的最大空闲时间 --> 28 <property name="maxIdleTime" value="20" /> 29 </bean> 30 31 <!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager类,该类实现了 32 PlatformTransactionManager接口,是针对采用数据源连接的特定实现 --> 33 <bean id="transactionManager" 34 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 35 <!-- 配置TransactionManager时需要注入数据源引用 --> 36 <property name="dataSource" ref="dataSource" /> 37 </bean> 38 39 <!-- 下面这个是前面定义的业务Bean --> 40 <bean id="newsDao" class="com.abc.dao.impl.NewsDaoImpl"> 41 <!-- 为业务Bean注入属性 --> 42 <property name="dataSource" ref="dataSource" /> 43 </bean> 44 45 <!-- 配置事务增强处理Bean,指定事务管理器 --> 46 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 47 <!-- 用于配置详细的事务语义 --> 48 <tx:attributes> 49 <!-- 所有以开头的方法都是read-only的,出现异常时回滚数据库 --> 50 <tx:method="get*" read-only="true" rollback-for="java.lang.Exception"/> 51 <!-- 其他方法使用默认的事务设置 --> 52 <tx:method="*" /> 53 </tx:attributes> 54 </tx:advice> 55 56 <!-- AOP配置的元素 --> 57 <aop:config> 58 <!-- 切点,匹配com.abc.dao.impl包下的所有以impl结尾的类里的所有方法的执行 --> 59 <aop:pointcut id="myPointcut" expression="(* com.abc.dao.impl.*Impl.*(..))" /> 60 <!-- 指定在txAdvice切点,使用txAdvice事务增强处理 --> 61 <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /> 62 </aop:config> 63</beans>
上面的配置文件中,在XMLSchema中启用了Spring配置文件的tx、aop两个命名空间,后面配置了一个事务增强处理,配置tx:advice../元素时只需要指定一个transaction-manager属性,该属性的默认值是"transactionManager"【提示:如果事务管理器Bean(PlatformTransactionManager的实现类)的名字是transactionManager,则配置tx:advice../元素时可以省略指定transaction-manager属性。只有当我们为事务管理器Bean指定了其他名字时,才需要为tx:advice../元素指定transaction-manager属性】。
配置文件最后一段是aop:config../的定义,它确保由txAdvice切面定义事务增强功能能在合适的点被织入。首先我们定义一个切点,命名为myPointcut,然后用一个Advisor把这个切入点与txAdvice绑定在一起,表示当myPointcut执行时,txAdvice定义的增强处理将被织入。
aop:advisor../元素是一个很奇怪的东西,它用于配置一个Advisor,但标准的 AOP 概念里并没有所谓的"Advisor",Advisor是Spring 1.X遗留下来的一个东西,Advisor的作用非常简单:将增强处理方法(Advice)和切入点(Pointcut)绑定在一起,保证Advice所包含的增强处理将在对应的切点方法执行时被织入。
当我们使用这种配置策略时,无需为每个业务Bean专门配置事务代理,Spring AOP会为业务组件自动生成代理,程序可以直接请求容器中的newsDao Bean,该Bean的方法已经具有了事务性——因为该Bean的实现类位于com.abc.dao.impl包下,而且类名以Impl结尾,和myPointcut切点的表达式匹配,故在指定里面的方法时,事务将被自动织入。下面是主程序:
1public class SpringTest { 2 public static void main(String[] arg) { 3 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 4 NewsDao dao = (NewsDao)context.getBean("newsDao",NewsDao.class); 5 dao.insert("新闻标题","新闻内容"); 6 } 7}
上面的程序中直接获取Spring中的NewsDao,并调用其insert方法,因为Spring AOP会为该Bean自动织入事务增强处理方式,所以newsDao Bean里的所有方法都具有事务性。
采用这种方式来配置事务还有一个额外的优势:Spring容器中只有一个newsDao,该newsDao已经具有了事务性,不像采用TransactionProxyFactoryBean策略时,容器中有一个目标Bean,还有为该目标Bean配置的事务代理Bean——当程序"不小心"获取了目标Bean后,如果调用目标Bean,那么此时Bean的方法时不具备事务性的,这可能埋下安全隐患。
采用tx:advisor../元素将Advice和切点绑定时,实际上是由Spring提供的Bean后处理器完成的。Spring提供了BeanNameAutoProxyCreator、DefaultAdvisorAutoProxyCreator两个Bean后处理器,它们都可以后处理容器中的Bean(为它们织入切面中包含的增强处理)。前面我们配置aop:advisor../元素时传入一个txAdvice事务增强处理,所以Bean后处理器将所有Bean实例里匹配切入点的方法织入事务操作的增强处理。
配置tx:advice../元素时除了需要指定一个transaction-manager属性以外,重要的是需要配置一个<attributes../>子元素,该子元素又可以包含多个<method../>元素。其实,配置tx:advice../元素的重点就是配置<method../>元素,每个<method../>子元素为一批方法指定所需的事务语义,包括事务传播属性、事务隔离属性、事务超时属性、只读事务、对指定异常回滚、对指定异常不回滚等。下面介绍<method../>元素可以指定的几个属性:
-
name:必选属性,与该事物语义关联的方法名。支持使用通配符,例如"get*","handle*","save*","on*Event"等
-
propagation:指定事务的传播行为,该属性值可以是Propagation枚举类的值的任意一种,各枚举值看这里
-
isolation:指定事务的隔离级别,该属性值可以是Isolation枚举类的值的任意一种,各枚举值的具体含义请参考API文档。该属性的默认值为Isolation.DEFAULT
-
timeout:指定事务超时时间(单位:秒)指定-1表示不会超时。该属性的默认值为-1
-
read-only:指定事务是否只读。该属性默认值为false
-
rollback-for:指定触发事务回滚的异常类(使用全限定名),该属性可以指定多个异常类,多个异常类用英文逗号隔开
-
no-rollback-for:指定不触发事务回滚的异常类(使用全限定名),该属性可以指定多个异常类,多个异常类用英文逗号隔开
如果我们想让事务在遇到特定的Checked异常时自动回滚,则可借助于rollback-for属性,例如:
1<tx:advice id="txAdvice" transaction-manager="transactionManager"> 2 <tx:attributes> 3 <!-- 所有以开头的方法都是read-only的,出现异常时回滚数据库 --> 4 <tx:method="get*" read-only="true" rollback-for="java.lang.Exception"/> 5 </tx:attributes> 6</tx:advice>
如果我们想让事务在出现某些特定的异常时不回滚,可使用no-rollback-for属性,例如:
1<tx:advice id="txAdvice" transaction-manager="transactionManager"> 2 <tx:attributes> 3 <!-- 出现某些特定的Exception,不回滚 --> 4 <tx:method="get*" read-only="true" 5 no-rollback-for="com.abc.exception.XxxException"/> 6 </tx:attributes> 7</tx:advice>
基于@Transactional注解
Spring除了支持XMLSchema方式配置事务以外,还支持使用@Transactional注解来配置事务。该注解既可以修饰Bean类,也可以修饰Bean方法。修饰类时,表明这些事务设置对整个Bean都起作用;修饰方法时,表明事务设置只对该方法有效。
和使用配置文件类似,使用@Transactional注解时,需要指定以下属性:
-
isolation:事务隔离级别。默认为底层事务的隔离级别
-
noRollbackFor:遇到指定异常时强制不回滚事务
-
noRollbackForClassName:遇到指定多个异常时强制不回滚事务,该属性值可以指定多个异常类名
-
propagation:事务传播属性
-
readOnly:事务是否只读
-
rollbackFor:遇到指定异常时强制回滚事务
-
rollbackForClassName:遇到指定异常时强制回滚事务,该属性值可以指定多个异常类名
-
timeout:事务的超时时长
下面使用@Transactional修饰需要添加事务的方法:
1public class NewsDaoImpl implements NewsDao { 2 @Transactional(propagation=Propagation.REQUIRED) 3 public void insert(String title, String content) { 4 //... 5 } 6}
上面的Bean类中insert方法使用了@Transactional修饰,表明该方法就会具有事务性。仅使用这个注解配置还不够,还需要在让Spring根据注解来配置事务。所以还需要在Spring的配置文件中增加如下配置片段:
1 <!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager类,该类实现了 2 PlatformTransactionManager接口,是针对采用数据源连接的特定实现 --> 3<bean id="transactionManager" 4 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 5 <!-- 配置TransactionManager时需要注入数据源引用 --> 6 <property name="dataSource" ref="dataSource" /> 7</bean> 8 9<tx:annotation-driven transaction-manager="transactionManager" />