Spring事务——Spring 2.X的事务配置策略

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<? 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定义的增强处理将被织入。

    <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切点的表达式匹配,故在指定里面的方法时,事务将被自动织入。下面是主程序:

?
1
2
3
4
5
6
7
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里的所有方法都具有事务性。

    采用这种方式来配置事务还有一个额外的优势: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
2
3
4
5
6
< 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属性,例如:

?
1
2
3
4
5
6
7
< 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修饰需要添加事务的方法:

?
1
2
3
4
5
6
public  class  NewsDaoImpl  implements  NewsDao {
     @Transactional (propagation=Propagation.REQUIRED)
     public  void  insert(String title, String content) {
         //...
     }
}

    上面的Bean类中insert方法使用了@Transactional修饰,表明该方法就会具有事务性。仅使用这个注解配置还不够,还需要在让Spring根据注解来配置事务。所以还需要在Spring的配置文件中增加如下配置片段:

?
1
2
3
4
5
6
7
8
9
  <!-- 配置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"  />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值