Spring 事务管理

 事务的概念:一组操作要么全完成,要么都不完成。

可以用ACID来描述事务:

  • Atomic:事务由一系列的活动构成,Atomic确保在一个事务中的这些活动要么全部发生、要么一个也不发生。
  • Consistent:事务完成之后(正常结束、非正常结束),应用程序的数据和状态处于合法的状态。
  • Isolated:事务允许多个用户操作相同的数据,并且不相互影响。因此事务执行需要被隔离,以防止并发的读写操作。一般意味要锁定数据库中的某个表、或者某个表的某些行
  • Durable:事务完成之后,事务的结果要是永久性的,不会因为系统的崩溃而消失。一般是将结果写入到数据库或者其他持久存储中。

ACID一般称为:原子性、一致性、隔离性、持久性

 

进一步阅读:For a more detailed explanation of transactions, I suggest that you read Martin Fowler’s Patterns of Enterprise Application Architecture (Addison-Wesley Professional,

2002). Specifically, chapter 5 discusses concurrency and transactions.

 

Spring事务原理

Unlike EJB, which is coupled with a Java Transaction API (JTA) implementation, Spring employs a callback mechanism that abstracts away the actual transaction implementation from the transactional code.

 



 Spring’s transacion managers



Spring提供基于平台的事务管理的Facade模式:如上图 。我们从最基本的DataSourceTransactionManager开始,它提供基于JDBC和iBatis的事务管理

1.DataSourceTransactionManager

它的配置如下:

<bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource" />
</bean>

 2.Hibernate transactions

其配置如下:

<bean id="transactionManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
         <property name="sessionFactory" ref="sessionFactory"/>
</bean>

 3.Java Persistence API transactions

配置如下:

<bean id="transactionManager"
  class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

 4.Java transaction API transactions

配置如下:

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManagerName" value="java:/TransactionManager" />
</bean>

 

 

 Programmatic transactions

使用TransactionTemplate来完成事务相关的动作,配置如下:为一个Bean注入TransactionTemplate。

<bean id="spitterService" class="com.habuma.spitter.service.SpitterServiceImpl">
...
    <property name="transactionTemplate ">
        <bean class="org.springframework.transaction.support.TransactionTemplate">
            <property name="transactionManager" ref="transactionManager" />
        </bean>
    </property>
</bean>

 这是一种侵入式的编程方式,使得代码和Spring完成相结合,所有一般使用声明式的事务:Declaring transactions

 

Declaring transactions

Spring的声明式的事务管理是通过Spring AOP来完成的;

使用@transactional:@transactional的5个属性:它们的取值范围都定义在TransactionDefinition中

  • 1. Propagation:传播行为。Propagation rules answer the question of whether a new transaction should be started or suspended, or if a method should even be executed within a transactional context at all。Propagation一共有7种不同取值:
    1.PROPAGATION_MANDATORY:    强制的,如果没有事务存在,则抛出异常
    2.PROPAGATION_NESTED:       嵌套的,如果现有事务存在,那么将允许在嵌套的事务中;否则其行为会由变得不定(有很多不同的实现)
    3.PROPAGATION_NEVER:        不能的,如果现有事务存在,则抛出异常
    4.PROPAGATION_NOT_SUPPORTED:不支持的,如果现有事务存在,那么在方法执行过程中暂停事务
    5.PROPAGATION_REQUIRED:     必须的,如果没有现有事务存在,那么创建一个新的,否则使用现有的事务
    6.PROPAGATION_REQUIRES_NEW: 新的,当前方法一定要允许在自己的事务中,而不管之前有没有事务存在
    7.PROPAGATION_SUPPORTS:     支持的,如果现在事务存在,那么允许其中;否则不使用事务
  • 2.Isolation levels:隔离等级。An isolation level defines how much a transaction may be impacted by the activities of other concurrent transactions.Think of it as how selfish the transaction is with the transactional data,多事务并发会造成如下情况:
    1.Dirty reads:occur when one transaction reads data that has been written but not yet committed by another transaction. If the changes are later rolled back, the data obtained by the first transaction will be invalid。
    
    2.Nonrepeatable reads:happen when a transaction performs the same query two or more times and each time the data is different. This is usually due to another concurrent transaction updating the data between the queries.
    
    3.Phantom reads:are similar to nonrepeatable reads. These occur when a transaction (T1) reads several rows, and then a concurrent transaction (T2) inserts rows. Upon subsequent queries, the first transaction (T1) finds additional rows that weren’t there before.
     事务的隔离等级和并发的效率成反比:In an ideal situation, transactions would be completely isolated from each other, thus avoiding these problems. But perfect isolation can affect performance because it often involves locking rows (and sometimes complete tables) in the data store.。所以需要根据实际业务来灵活变通事务的隔离机制。Isolation levels一共有4种不同的等级
    1.ISOLATION_DEFAULT
    2.ISOLATION_READ_UNCOMMITTED
    3.ISOLATION_READ_COMMITTED
    4.ISOLATION_REPEATABLE_READ
    5.ISOLATION_SERIALIZABLE
  • 3.READ-ONLY标识一个事务是否是READ-ONLY的。当事务开始时,设置此表示,有助于优化事务,仅用于可能新建事务的Propagation中:PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW, and PROPAGATION_NESTED
  • 4.Transaction timeout:超时时间。you can declare a transaction to automatically roll back after a certain number of seconds.当事务开始的时候计时,因此也要求Propagation是可能新创建事务的:和上面相同的三个
  • 5.ROLLBACK RULES:回滚规则。a set of rules that define which exceptions prompt a rollback and which ones don’t. 默认情况下只在runtime异常发生时回滚,而不再checked 异常发生时回滚。 

 基于XML的事务:

<tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="*" propagation="SUPPORTS"read-only="true"/>
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:advisor pointcut="execution(* *..SpitterService.*(..))" advice-ref="txAdvice"/>
</aop:config>

 

基于注解的事务:

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

 @Transactional:either at the class level or at the method level

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值