`
zengjinliang
  • 浏览: 301995 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Spring事务处理

阅读更多
Spring中事务的定义:
一、Propagation :

对于特定的方法或方法命名模式,代理的具体事务行为由事务属性驱动,如下面的例子所示:
<prop key="load*"></prop> PROPAGATION_REQUIRED,readOnly
<prop key="store*"></prop> PROPAGATION_REQUIRED

  key属性确定代理应该给哪个方法增加事务行为。这样的属性最重要的部份是传播行为。有以下选项可供使用:

  • PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
  • PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
  • PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
  • PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
  • PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
  • PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。

      前六个策略类似于EJB CMT:常量名相同,因此,对EJB开发人员来说,应该立刻就感到熟悉。第七个(PROPAGATION_NESTED)是Spring所提供的一个特殊变量。它要求事务管理器或者使用JDBC 3.0 Savepoint API提供嵌套事务行为(如Spring的DataSourceTransactionManager),或者通过JTA支持嵌套事务。

    

二、Isolation Level(事务隔离等级):
1、Serializable:最严格的级别,事务串行执行,资源消耗最大;
2、REPEATABLE READ:保证了一个事务不会修改已经由另一个事务读取但未提交(回滚)的数据。避免了“脏读取”和“不可重复读取”的情况,但是带来了更多的性能损失。
3、READ COMMITTED:大多数主流数据库的默认事务等级,保证了一个事务不会读到另一个并行事务已修改但未提交的数据,避免了“脏读取”。该级别适用于大多数系统。
4、Read Uncommitted:保证了读取过程中不会读取到非法数据。

spring中的Isolation属性:
1、ISOLATION_DEFAULT :使用当前数据源的默认级别
2、ISOLATION_READ_UNCOMMITTED :Dirty reads, non-repeatable reads, and phantom reads can occur.
3、ISOLATION_READ_COMMITTED :Dirty reads are prevented; non-repeatable reads and phantom reads can occur.
4、ISOLATION_REPEATABLE_READ:Dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5、ISOLATION_SERIALIZABLE:Dirty reads, non-repeatable reads, and phantom reads are prevented.

三、readOnly
事务属性中的readOnly标志表示对应的事务应该被最优化为只读事务。这是一个最优化提示。在一些情况下,一些事务策略能够起到显著的最优化效果,例如在使用Object/Relational映射工具(如:Hibernate或TopLink)时避免dirty checking(试图“刷新”)。

四、Timeout

      在事务属性中还有定义“timeout”值的选项,指定事务超时为几秒。在JTA中,这将被简单地传递到J2EE服务器的事务协调程序,并据此得到相应的解释。

事务划分策略

1、推荐在业务层使用事务,这样可以允许业务层捕获导致rollback的异常,并抛出恰当的业务层异常;不在dao层使用事务是因为这会限制了dao重用其他事务需求,并且dao层没有实现业务逻辑,并且原子性也是业务层的概念。

spring声明性事务的划分:
1、有四个地方需要配置:The four participants are transaction manager, proxy factory, transaction interceptor, and a set of transaction attributes.




2、使用ProxyFactoryBean/Transaction Interceptor(transactionInterceptor)配置spring事务

以下为配置实例:

<!---->
  <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource" destroy-method="close"></bean>
    <property name="driverClassName">
      <value></value>${jdbc.driverClassName}
    </property>
    <property name="url"><value></value>${jdbc.url}</property>
    <property name="username"><value></value>${jdbc.username}</property>
    <property name="password"><value></value>${jdbc.password}</property>
  
   
  <!---->
  <bean class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao" id="dao"></bean>
    <property name="dataSource">
      <ref local="dataSource"></ref>
    </property> 
  
   
  <!---->
  <bean id="transactionManager" span=""></bean>
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
      <ref local="dataSource"></ref>
    </property> 
  
   
  <!---->
  <bean id="transactionInterceptor" span=""></bean>
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager"></property>
      <ref bean="transactionManager"></ref>
    
    <property name="transactionAttributeSource"></property>
    
   
  <!---->
  <bean id="boxOffice" span=""></bean>
        class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target"></property>
      <ref local="boxOfficeTarget"></ref>
    
    <property name="proxyInterfaces"></property>
      <value></value>org.springframework.prospring.ticket.service.BoxOffice
    
    <property name="interceptorNames"></property>
      <value></value>transactionInterceptor
    
    
   
  <!---->
  <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl" id="boxOfficeTarget"></bean>
    <property name="boxOfficeDao">
      <ref local="dao"></ref>
    </property> 
  
      <value></value>
org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS,re
adOnly
org.springframework.prospring.ticket.service.BoxOffice.allocate*=PROPAGATION_REQUIR
ED
      
    

3、使用TransactionProxyFactoryBean配置spring事务
以下为配置实例:

  <!---->
  <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource" destroy-method="close"></bean>
    <property name="driverClassName">
      <value></value>${jdbc.driverClassName}
    </property>
    <property name="url"><value></value>${jdbc.url}</property>
    <property name="username"><value></value>${jdbc.username}</property>
    <property name="password"><value></value>${jdbc.password}</property>
  
   
  <!---->
  <bean class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao" id="dao"></bean>
    <property name="dataSource">
      <ref local="dataSource"></ref>
    </property> 
  
   
  <!---->
  <bean id="transactionManager" span=""></bean>
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
      <ref local="dataSource"></ref>
    </property> 
  
   
  <!---->
  <bean id="boxOffice" span=""></bean>
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager"><ref bean="transactionManager"></ref></property>
    <property name="target"></property>
    <property name="transactionAttributes"></property>
      <props></props>
        <prop key="get*"></prop>PROPAGATION_SUPPORTS,readOnly
        <prop key="allocate*"></prop>PROPAGATION_REQUIRED
      
    
    
      <bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl"></bean>
        <property name="boxOfficeDao">
          <ref local="dao"></ref>
        </property> 
      
    

4、使用BeanNameAutoProxyCreator配置spring事务
如果有大量的bean需要使用事物,那么只要在配置文件中提供bean name给BeanNameAutoProxyCreator,spring就会个给该bean提供事务代理,配置实例如下:

  <!---->
  <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource" destroy-method="close"></bean>
    <property name="driverClassName">
      <value></value>${jdbc.driverClassName}
    </property>
    <property name="url"><value></value>${jdbc.url}</property>
    <property name="username"><value></value>${jdbc.username}</property>
    <property name="password"><value></value>${jdbc.password}</property>
  
   
  <!---->
  <bean class="org.springframework.prospring.ticket.dao.jdbc.JdbcBoxOfficeDao" id="dao"></bean>
    <property name="dataSource">
      <ref local="dataSource"></ref>
    </property> 
  
   
  <!---->
  <bean id="transactionManager" span=""></bean>
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource">
      <ref local="dataSource"></ref>
    </property> 
   
   
  <!---->
  <bean id="transactionInterceptor" span=""></bean>
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager"></property>
      <ref bean="transactionManager"></ref>
    
    <property name="transactionAttributeSource"></property>
      <value></value>
org.springframework.prospring.ticket.service.BoxOffice.get*=PROPAGATION_SUPPORTS
,readOnly
org.springframework.prospring.ticket.service.BoxOffice.allocate*=
PROPAGATION_REQUIRED
      
    
    
   
  <!---->
<bean id="autoProxyCreator" span=""></bean>
   class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="interceptorNames"></property>
    <value></value>transactionInterceptor
  
  <property name="beanNames"></property>
    <list></list>
      <idref local="boxOffice"></idref>
    
  
  
   
<!---->
<bean class="org.springframework.prospring.ticket.service.BoxOfficeImpl" id="boxOffice"></bean>
  <property name="boxOfficeDao">
    <ref local="dao"></ref>
  </property> 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics