Spring配置事务的五种方式

 

 Java事务的类型有三种:spring

JDBC事务、 能够将多个 SQL 语句结合到一个事务中。JDBC 事务的一个缺点是事务的范围局限于一个数据库链接。一个 JDBC 事务不能跨越多个数据库数据库

JTA(Java Transaction API)事务、事务能够跨越多个数据库或多个DAO,使用也比较复杂。express

容器事务。主要指的是J2EE应用服务器提供的事务管理,局限于EJB应用使用。编程

 

spring事务的配置方式编程式事务和声明式事务,相信你们都知道是有5种,但咱们常常使用的应该就是基于注解和tx标签配置拦截器两种方式了服务器

 

 

1
2
3
4
5
6
7
8
9
10
    < bean  id = "sessionFactory"
          class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >   
         < property  name = "configLocation"  value = "classpath:hibernate.cfg.xml"  />   
         < property  name = "configurationClass"  value = "org.hibernate.cfg.AnnotationConfiguration"  /> 
     </ bean >
<!-- 定义事务管理器(声明式的事务) -->
   < bean  id = "transactionManager"
       class = "org.springframework.orm.hibernate3.HibernateTransactionManager"
       < property  name = "sessionFactory"  ref = "sessionFactory"  /> 
   </ bean >

ps:声明式事务管理创建在AOP之上的。其本质是对方法先后进行拦截,而后在目标方法开始以前建立或者加入一个事务,在执行完目标方法以后根据执行状况提交或者回滚事务。session

 

一、基于注解,DAO上需加上@Transactional注解spa

 

1
< tx:annotation-driven  transaction-manager = "transactionManager" />


二、使用tx标签配置的拦截器hibernate

 

1
2
3
4
5
6
7
8
9
10
11
12
< tx:advice  id = "txAdvice"  transaction-manager = "transactionManager"
     < tx:attributes
         < tx:method  name = "*"  propagation = "REQUIRED"  /> 
     </ tx:attributes
</ tx:advice
 
< aop:config
     < aop:pointcut  id = "interceptorPointCuts"
         expression = "execution(* com.bluesky.spring.dao.*.*(..))"  /> 
     < aop:advisor  advice-ref = "txAdvice"
         pointcut-ref = "interceptorPointCuts"  />         
</ aop:config >

三、使用拦截器代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
< bean  id = "transactionInterceptor"
        class = "org.springframework.transaction.interceptor.TransactionInterceptor" >   
        < property  name = "transactionManager"  ref = "transactionManager"  />   
        <!-- 配置事务属性 -->
        < property  name = "transactionAttributes" >   
            < props >   
                < prop  key = "*" >PROPAGATION_REQUIRED</ prop >   
            </ props >   
        </ property >   
    </ bean
 
    < bean  class = "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >   
        < property  name = "beanNames" >   
            < list >   
                < value >*Dao</ value
            </ list >   
        </ property >   
        < property  name = "interceptorNames" >   
            < list >   
                < value >transactionInterceptor</ value >   
            </ list >   
        </ property >   
    </ bean >

 

四、全部Bean共享一个代理基类code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
< bean  id = "transactionBase"
         class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
         lazy-init = "true"  abstract = "true" >   
     <!-- 配置事务管理器 -->
     < property  name = "transactionManager"  ref = "transactionManager"  />   
     <!-- 配置事务属性 -->
     < property  name = "transactionAttributes" >   
         < props >   
             < prop  key = "*" >PROPAGATION_REQUIRED</ prop >   
         </ props >   
     </ property >   
</ bean >
     <!-- 配置DAO -->
     < bean  id = "userDaoTarget"  class = "com.bluesky.spring.dao.UserDaoImpl"
         < property  name = "sessionFactory"  ref = "sessionFactory"  /> 
     </ bean
 
     < bean  id = "userDao"  parent = "transactionBase"  >   
         < property  name = "target"  ref = "userDaoTarget"  />    
     </ bean >

 

五、每一个Bean都有一个代理

1
2
3
4
5
6
7
8
9
10
11
12
13
< bean  id = "userDao"
         class = "org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >   
            <!-- 配置事务管理器 -->
            < property  name = "transactionManager"  ref = "transactionManager"  />      
         < property  name = "target"  ref = "userDaoTarget"  />   
          < property  name = "proxyInterfaces"  value = "com.bluesky.spring.dao.GeneratorDao"  /> 
         <!-- 配置事务属性 -->
         < property  name = "transactionAttributes" >   
             < props >   
                 < prop  key = "*" >PROPAGATION_REQUIRED</ prop
             </ props >   
         </ property >   
     </ bean >
相关文章
相关标签/搜索