Spring 2.X的事务配置策略java
虽然前面介绍的TransactionProxyFactoryBean配置策略简单易懂,但配置起来极为麻烦:每一个目标Bean都须要配置一个TransactionProxyFactoryBean代理,这种方式将致使配置文件急剧增长。mysql
Spring 2.X的XMLSchema方式提供了更简洁的事务配置策略,Spring 2.X提供了tx命名空间来配置事务管理,tx命名空间提供了<tx:advice../>元素来配置事务加强处理,一旦使用该元素配置了加强处理,就能够直接使用<aop:advisor../>元素启用自动代理了。
spring
下面的应用示例依然使用前面(点击查看)提供的NewsDao接口和NewsDaoImpl实现类,但改成使用<tx:advice../>元素来配置事务加强处理,再使用<tx:advisor../>为容器中一批Bean配置自动事务代理。下面是配置文件:
sql
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/aop/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 使用C3P0数据库链接池做为数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPolledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost/test" /> <property name="user" value="root" /> <property name="password" value="root" /> <property name="maxPoolSize" value="40" /> <property name="minPoolSize" value="4" /> <property name="initialPoolSize" value="10" /> <!-- 指定数据库链接池的链接的最大空闲时间 --> <property name="maxIdleTime" value="20" /> </bean> <!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager类,该类实现了 PlatformTransactionManager接口,是针对采用数据源链接的特定实现 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 配置TransactionManager时须要注入数据源引用 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 下面这个是前面定义的业务Bean --> <bean id="newsDao" class="com.abc.dao.impl.NewsDaoImpl"> <!-- 为业务Bean注入属性 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务加强处理Bean,指定事务管理器 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 用于配置详细的事务语义 --> <tx:attributes> <!-- 全部以开头的方法都是read-only的,出现异常时回滚数据库 --> <tx:method="get*" read-only="true" rollback-for="java.lang.Exception"/> <!-- 其余方法使用默认的事务设置 --> <tx:method="*" /> </tx:attributes> </tx:advice> <!-- AOP配置的元素 --> <aop:config> <!-- 切点,匹配com.abc.dao.impl包下的全部以impl结尾的类里的全部方法的执行 --> <aop:pointcut id="myPointcut" expression="(* com.abc.dao.impl.*Impl.*(..))" /> <!-- 指定在txAdvice切点,使用txAdvice事务加强处理 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut" /> </aop:config> </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定义的加强处理将被织入。
express
<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切点的表达式匹配,故在指定里面的方法时,事务将被自动织入。下面是主程序:spa
public class SpringTest { public static void main(String[] arg) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); NewsDao dao = (NewsDao)context.getBean("newsDao",NewsDao.class); dao.insert("新闻标题","新闻内容"); } }
上面的程序中直接获取Spring中的NewsDao,并调用其insert方法,由于Spring AOP会为该Bean自动织入事务加强处理方式,因此newsDao Bean里的全部方法都具备事务性。
.net
采用这种方式来配置事务还有一个额外的优点: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属性,例如:
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 全部以开头的方法都是read-only的,出现异常时回滚数据库 --> <tx:method="get*" read-only="true" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice>
若是咱们想让事务在出现某些特定的异常时不回滚,可以使用no-rollback-for属性,例如:
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 出现某些特定的Exception,不回滚 --> <tx:method="get*" read-only="true" no-rollback-for="com.abc.exception.XxxException"/> </tx:attributes> </tx:advice>
基于@Transactional注解
Spring除了支持XMLSchema方式配置事务之外,还支持使用@Transactional注解来配置事务。该注解既能够修饰Bean类,也能够修饰Bean方法。修饰类时,代表这些事务设置对整个Bean都起做用;修饰方法时,代表事务设置只对该方法有效。
和使用配置文件相似,使用@Transactional注解时,须要指定如下属性:
isolation:事务隔离级别。默认为底层事务的隔离级别
noRollbackFor:遇到指定异常时强制不回滚事务
noRollbackForClassName:遇到指定多个异常时强制不回滚事务,该属性值能够指定多个异常类名
propagation:事务传播属性
readOnly:事务是否只读
rollbackFor:遇到指定异常时强制回滚事务
rollbackForClassName:遇到指定异常时强制回滚事务,该属性值能够指定多个异常类名
timeout:事务的超时时长
下面使用@Transactional修饰须要添加事务的方法:
public class NewsDaoImpl implements NewsDao { @Transactional(propagation=Propagation.REQUIRED) public void insert(String title, String content) { //... } }
上面的Bean类中insert方法使用了@Transactional修饰,代表该方法就会具备事务性。仅使用这个注解配置还不够,还须要在让Spring根据注解来配置事务。因此还须要在Spring的配置文件中增长以下配置片断:
<!-- 配置JDBC数据源的局部事务管理器,使用DataSourceTransactionManager类,该类实现了 PlatformTransactionManager接口,是针对采用数据源链接的特定实现 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 配置TransactionManager时须要注入数据源引用 --> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" />