spring Transaction

 

事务 ( acid )

  atomic : 原子性 ,有多个行为组成的一组工作单元,这组操作要么都发生,要么都不发

生。

  consistent : 一致性 事务结束之后(不管成功或者失败 ) 系统所处的状态和他的业务规

则应当是一致的

  isolated : 隔离性,不同的事务之间不能相互干扰,这通常要以为着对数据库加锁

  duration : 持久性,一旦事务提交,事务的结果就会持久保存,无论什么样的系统崩溃,

他们都将幸免遇难

Spring的事物支持

Spring 提供了对程序控制事务的支持,同时也支持声明式事务Spring 采取了一种回调机制,将事务的控制从应用中抽取出来。选择程序控制或者声明式事务取决于事务控制的粒度。

SpringTransactionManager

针对不同的事务实现,Spring提供了不同的事务管理工具

JDBC :   org.springframework.jdbc.datasource.DataSourceTransactionManager

Hibernate: org.springframework.orm.hibernate.HibernateTransactionManager .

 

** 每种管理器都充当了针对特定平台的事务实现代理

Spring中通过程序控制事务

 

编程式事物主要依靠TransactionTemplate接口来完成。

TransactionTemplate.execute();

 

1)声明事物管理器( transaction manager ) : 提供基本的事物服务。

2)声明事物模板( TransactionTemplate) ,并向TransactionTemplate中注入事物管理器

3) 向业务组件中注入TransactionTemplate

4)在业务组件中通过TransactionTemplateexecute方法来进行事物控制。

 

1) 声明事务管理器, 在事务管理器中注入DataSource SessionFactory , 这取决于事务管理器的类型:

<bean id="transactionManager"  

     class="org.springframework.orm.hibernate3.HibernateTransactionManager">

      <property name="sessionFactory">

                     <ref bean="sessionFactory"/>

      </property>

</bean>

 

 

2) 声明事务模板类 , 为事务模板类注入事务管理器 :

  <bean id="transactionTemplate"

       class="org.springframework.transaction.support.TransactionTemplate">

      <property name="transactionManager">

         <ref bean="transactionManager"/>

      </property>

   </bean>

在事物模板类( TransactionTemplate )中定义了一个方法

template.execute( TransationCallBack callBack ) ;

 

需要运行在事务中的代码因该编写在 TransactionCallBack 。在这个模型里我们可以这样理解几种组件之间的关系

TransactionManager : 提供基本的事务服务 :通过TransactionManager,完成对事物的

                       提交或会滚

transactionTemplate : 为需要事务服务的方法添加事务处理功能。具体的事务操作会调

transactionManager完成

TransactionCallBack ; 提供运行在事务中的代码,TransactionCallBack提供的代码会被

Template加上事务环境

3) 编写事务模板所需要的回调接口 TransactionCallBack 该接口通常以匿名内部类的方式实现。

     public interface TransactionCallBack{

              Object doInTransaction( TransactionStatus ts )

     }

 

将需要放在事务中的代码写在doInTransaction方法中

public class AdminService{   

       private TransactionTemplate transactionTemplate = null ;

       private AdminDao dao = null ;

       ....... ;

       public void createAdmins(final List<Admin> admins){

          transactionTemplate.execute( new TransactionCallback(){

                      public Object doInTransaction( TransactionStatus ts ){

                          try{

                             for( Admin a : admins){

                             dao.insert( a );

                           }catch( Exception e){

                                ts.setRollbackOnly() ;

                        }

                           return null ;

                       }});

     }

}

  ** dao transactionTemplate 是通过依赖注入 AdminService方法中的。

 

声明式事务

 

Spring中使用声明式事务我们需要使用TransactionProxyFactoryBean .这个Bean的作用是为目标对象生成代理,在生成的代理中,会将目标的方法包装在事务上下文中

 

<bean id="adminService"

         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

     <property name="proxyInterfaces">

           <list>

                            <value>package.AdminService</value>

           </list>

     </property>

     <property name="target">

          <ref bean="xxxx"/>

     </property>

 

     <property name="transactionManager">

           <bean ref=""/>

     </property>

     <!-- 事务属性 -->

     <property name="transactionAttributeResource">

          <bean ref=""/>

     </property>

</bean>

 

事物属性

所谓事务属性,是对事务策略如何应用到方法中的描述 。这些描述包括这样一些属性

  * 传播行为

  * 只读提示

  * 隔离级别

  * 事务超时间隔

 

声明简单的事务策略

TransactionProxyFactoryBean 有一个transactionAttributeSource 属性,这个属性就是

用于指定事务策略的 。该接口定义如下

 

 

public interface TransactionAttributeSource{

      TransactionAttribute getTransactionAttribute( Method , targetClass);

}

getTransactionAttribute方法根据目标类和方法获得事务属性

spring为此接口提供了一个默认实现, MatchAlwaysTransactionAttributeSource

该接口在配置文件中的配置如下

<bean id="transactionAttributeSource" class="org.springframework.transaction

.interceptor.MatchAlwaysTransactionAttributeSource"

</bean>

 

在指定该策略时,我们没有指定采用什么样的事物策略,也没有指定哪个类的那个方法需要使用这个策略,因为MatchAlwaysTransactionAttributeSource 总是返回相同的事务策略而不管这个事务中包含了哪些方法。

默认的事物属性是 (PROPAGATION_REQUIRED | ISOLATION_DEFAULT)

 :

替换默认的事物策略

MatchAlwaysTransactionAttributeSource注入DefaultTransactionAttribute,

DefaultTransactionActtibute中,制定我们需要的事物属性。

 

<bean id="defaultTransactionAttribute" class="org.springframework.transaction.interceptor.

                           DefualtTransactionAttribute">

 

       <property name="propagationBehaviorName">

                     <value>PROPAGATION_REQUIRED_NEW</value>

       </property>

 

      <property name="isolationLevelName">

                     <value>ISOLATION_READ_COMMITED</value>

      </property>

</bean>

 

MatchAlwaysTransactionAttributeSource 注入自定义的事务属性

<bean id="transactionAttributeSource"  class="org.springframework.transaction.interceptor

.MatchAlwaysTransactionAttributeSource">

   <property name="transactionAttribute">

      <ref bean="defaultTransactionAttribute"/>

   </property>

</bean>

 

为不同的方法指定不同的事物策略

MatchAlwaysTransactionAttributeSource 对每一个方法均采用相同的事务属性,这对于

简单应用时十分方便的 但是如果在不同方法需要采用不同事务属性的情况下,这种方式

就不适用 了,我们可以通过另外一种TransactionAttributeSource来为每一个方法声明事务

属性

 

<bean id="transactionAttributeSource"

    class="org.springframework.transction.interceptor.NameMatchTransactionAttributeSource">

        <property name="properties">

           <props>

                 <prop key="methodName">

                    PROPAGATION_REQUIRED_NEW,ISOLATION_READ_COMMITED

                  </prop>

           </props>

   </property>

</bean>

 

在指定方法名称时,可以使用通配符简化声明方式

<property name="properties">

  <props>

     <prop key="delete*">

           PROPAGATION_REQUIRED_NEW,ISOLATION_READ_COMMITED

     </prop>

  </props>

</property>

 

指定事务回滚策略

当事务执行的过程中出现异常时,事务可以被声明为回滚或者不回滚,默认规则是RutimeException事务回滚,CheckedException 事务不会滚,但是可以通过配置改变策略。

<bean id="transactionAttributeSource"

    class="org.springframework.transaction.interceptor.NameMathTransactionAttributeSource">

   

<property name="properties">

         <props>

               <prop name="methodname">

                 PROPAGATION_REQUIRED,-DAOException

             </prop>

         </props>

   </property>

</bean>

 

DAOExcepton前面使用+ - 修饰

  + 表示异常发生时提交事务。

  - 表示异常发生时会滚事务。

 

名称匹配事务的捷径

   以前我们是通过为TransactionProxyFactoryBean 注入TransactionAttributeSource来指定

事务属性,还用另外一种更加简便的方法来指定事务属性,在这种方式中,不需要定义单独的TransactionAttributeSource

 

<bean id="adminService"

        class="org.springframework.transaction.interceptor.TransactionFactoryBean">

     <property name="transactionProperties">

        <props>

               <prop key="mehtodName">

                  PROPOAGATION_REQUIRED,ISOLATION_READ_COMMITED

               </prop>

       </props>

     </property>

</bean>

 

实现声明式事务的一般步骤

1)编写目标对象,即要运行在事物中的对象,通常为业务接口的实现类

2) 根据持久化技术的不同,声明transactionManager.

3)  为目标对象声明代理 ( TrnasactionProxyFactoryBean )并为其注入以下属性

        a, proxyInterfaces

     b,target

     c,transactionManager

     d, transacitonAttribute 或者 transactionProperteis.

 

1)      声明目标对象

    <bean id="adminServiceTarget" class="xxxx.AdminServiceImpl">

          <property name="adminDao">

                            <ref bean=""/>

          </property>

   </bean>

 

 

 

2)      声明事务管理器

 

<bean id="transactionManager"

       class="org.springframework.orm.hibernate3.HibernateTransactionManager">

 

      <property name="sessionFactory">

               <ref bean="xxxx"/>

      </property>

  </bean>

 

3) 声明TransactionAttributeSource来指定事务属性,有两种常用事务属性实现可以采用

   <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.

MatchAlwaysTransactionAttributeSource">

      <property name="properties">

           <props>

                            <prop key="mehtodName">

                             xxxxxxxxxxxxxxxx ;

                 </prop>

           </props>

          </property>

      </bean>

 

4) 通过TransactionProxFactoryBean 将事务控制织入到目标对象的代理中

 

   <bean id="adminService"

      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

         <property name="proxyInterfaces">

               <list>

                 <value>xxx.AdminService</value>

               </list>

         </property>

       <property name="transactionManager">

                <ref bean="transactionManager"/>

        </property>

       <property name="target">

              <ref bane="adminServiceTarget"/>

        </property>

        <property name="transactionAttributeSource">

               <ref bean="transactionAttributeSource"/>

        </property>

   </bean>

 

 

 

事务属性 :

 

     transactionAttributeSource :

        |- MatchAlwaysTransactionAttributeResource :

        |     不管什么方法,什么类型总是返回相同的事务属性 对于简单

        |     应用非常方便,但是对于不同的方法,需要采用不同的事物属性

        |     时就不适用了。

        |-  NameMatchTransactionAttributeResource

             可以根据不同的方法名称,指定不同的事务属性

 

 

 

 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值