Spring事务管理与传播机制详解以及使用实例

写这篇博客之前我首先读了《spring in action》,之后在网上看了一些关于Spring事务管理的文章,感觉都没有讲全,这里就将书上的和网上关于事务的知识总结一下,参考的文章如下:

1 初步理解

理解事务之前,先讲一个你日常生活中最常干的事:取钱。 
比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱;然后ATM出1000元钱。这两个步骤必须是要么都执行要么都不执行。如果银行卡扣除了1000块但是ATM出钱失败的话,你将会损失1000元;如果银行卡扣钱失败但是ATM却出了1000块,那么银行将损失1000元。所以,如果一个步骤成功另一个步骤失败对双方都不是好事,如果不管哪一个步骤失败了以后,整个取钱过程都能回滚,也就是完全取消所有操作的话,这对双方都是极好的。 
事务就是用来解决类似问题的。事务是一系列的动作,它们综合在一起才是一个完整的工作单元,这些动作必须全部完成,如果有一个失败的话,那么事务就会回滚到最开始的状态,仿佛什么都没发生过一样。 
在企业级应用程序开发中,事务管理必不可少的技术,用来确保数据的完整性和一致性。 
事务有四个特性:ACID

  • 原子性(Atomicity):事务是一个原子操作,由一系列动作组成。事务的原子性确保动作要么全部完成,要么完全不起作用。
  • 一致性(Consistency):一旦事务完成(不管成功还是失败),系统必须确保它所建模的业务处于一致的状态,而不会是部分完成部分失败。在现实中的数据不应该被破坏。
  • 隔离性(Isolation):可能有许多事务会同时处理相同的数据,因此每个事务都应该与其他事务隔离开来,防止数据损坏。
  • 持久性(Durability):一旦事务完成,无论发生什么系统错误,它的结果都不应该受到影响,这样就能从任何系统崩溃中恢复过来。通常情况下,事务的结果被写到持久化存储器中。

2 核心接口

Spring事务管理的实现有许多细节,如果对整个接口框架有个大体了解会非常有利于我们理解事务,下面通过讲解Spring的事务接口来了解Spring实现事务的具体策略。 
Spring事务管理涉及的接口的联系如下:

这里写图片描述

2.1 事务管理器

Spring并不直接管理事务,而是提供了多种事务管理器,他们将事务管理的职责委托给hibernate或者JTA等持久化机制所提供的相关平台框架的事务来实现。 
Spring事务管理器的接口是org.springframework.transaction.PlatformTransactionManager,通过这个接口,Spring为各个平台如JDBC、Hibernate等都提供了对应的事务管理器,但是具体的实现就是各个平台自己的事情了。此接口的内容如下:

 
  1. Public interface PlatformTransactionManager()...{

  2. // 由TransactionDefinition得到TransactionStatus对象

  3. TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;

  4. // 提交

  5. Void commit(TransactionStatus status) throws TransactionException;

  6. // 回滚

  7. Void rollback(TransactionStatus status) throws TransactionException;

  8. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

从这里可知具体的具体的事务管理机制对Spring来说是透明的,它并不关心那些,那些是对应各个平台需要关心的,所以Spring事务管理的一个优点就是为不同的事务API提供一致的编程模型,如JTA、JDBC、Hibernate、JPA。下面分别介绍各个平台框架实现事务管理的机制。

2.1.1 JDBC事务

如果应用程序中直接使用JDBC来进行持久化,DataSourceTransactionManager会为你处理事务边界。为了使用DataSourceTransactionManager,你需要使用如下的XML将其装配到应用程序的上下文定义中:

 
  1. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  2. <property name="dataSource" ref="dataSource" />

  3. </bean>

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

实际上,DataSourceTransactionManager是通过调用Java.sql.Connection来管理事务,而后者是通过DataSource获取到的。通过调用连接的commit()方法来提交事务,同样,事务失败则通过调用rollback()方法进行回滚。

2.1.2 Hibernate事务

如果应用程序的持久化是通过Hibernate实习的,那么你需要使用HibernateTransactionManager。对于Hibernate3,需要在Spring上下文定义中添加如下的<bean>声明:

 
  1. <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

  2. <property name="sessionFactory" ref="sessionFactory" />

  3. </bean>

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

sessionFactory属性需要装配一个Hibernate的session工厂,HibernateTransactionManager的实现细节是它将事务管理的职责委托给org.hibernate.Transaction对象,而后者是从Hibernate Session中获取到的。当事务成功完成时,HibernateTransactionManager将会调用Transaction对象的commit()方法,反之,将会调用rollback()方法。

2.1.3 Java持久化API事务(JPA)

Hibernate多年来一直是事实上的Java持久化标准,但是现在Java持久化API作为真正的Java持久化标准进入大家的视野。如果你计划使用JPA的话,那你需要使用Spring的JpaTransactionManager来处理事务。你需要在Spring中这样配置JpaTransactionManager:

 
  1. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

  2. <property name="sessionFactory" ref="sessionFactory" />

  3. </bean>

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

JpaTransactionManager只需要装配一个JPA实体管理工厂(javax.persistence.EntityManagerFactory接口的任意实现)。JpaTransactionManager将与由工厂所产生的JPA EntityManager合作来构建事务。

2.1.4 Java原生API事务

如果你没有使用以上所述的事务管理,或者是跨越了多个事务管理源(比如两个或者是多个不同的数据源),你就需要使用JtaTransactionManager:

 
  1. <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">

  2. <property name="transactionManagerName" value="java:/TransactionManager" />

  3. </bean>

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

JtaTransactionManager将事务管理的责任委托给javax.transaction.UserTransaction和javax.transaction.TransactionManager对象,其中事务成功完成通过UserTransaction.commit()方法提交,事务失败通过UserTransaction.rollback()方法回滚。

2.2 基本事务属性的定义

上面讲到的事务管理器接口PlatformTransactionManager通过getTransaction(TransactionDefinition definition)方法来得到事务,这个方法里面的参数是TransactionDefinition类,这个类就定义了一些基本的事务属性。 
那么什么是事务属性呢?事务属性可以理解成事务的一些基本配置,描述了事务策略如何应用到方法上。事务属性包含了5个方面,如图所示:

这里写图片描述

而TransactionDefinition接口内容如下:

 
  1. public interface TransactionDefinition {

  2. int getPropagationBehavior(); // 返回事务的传播行为

  3. int getIsolationLevel(); // 返回事务的隔离级别,事务管理器根据它来控制另外一个事务可以看到本事务内的哪些数据

  4. int getTimeout(); // 返回事务必须在多少秒内完成

  5. boolean isReadOnly(); // 事务是否只读,事务管理器能够根据这个返回值进行优化,确保事务是只读的

  6. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

我们可以发现TransactionDefinition正好用来定义事务属性,下面详细介绍一下各个事务属性。

2.2.1 传播行为

事务的第一个方面是传播行为(propagation behavior)。当事务方法被另一个事务方法调用时,必须指定事务应该如何传播。例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的事务中运行。Spring定义了七种传播行为:

传播行为含义
PROPAGATION_REQUIRED表示当前方法必须运行在事务中。如果当前事务存在,方法将会在该事务中运行。否则,会启动一个新的事务
PROPAGATION_SUPPORTS表示当前方法不需要事务上下文,但是如果存在当前事务的话,那么该方法会在这个事务中运行
PROPAGATION_MANDATORY表示该方法必须在事务中运行,如果当前事务不存在,则会抛出一个异常
PROPAGATION_REQUIRED_NEW表示当前方法必须运行在它自己的事务中。一个新的事务将被启动。如果存在当前事务,在该方法执行期间,当前事务会被挂起。如果使用JTATransactionManager的话,则需要访问TransactionManager
PROPAGATION_NOT_SUPPORTED表示该方法不应该运行在事务中。如果存在当前事务,在该方法运行期间,当前事务将被挂起。如果使用JTATransactionManager的话,则需要访问TransactionManager
PROPAGATION_NEVER表示当前方法不应该运行在事务上下文中。如果当前正有一个事务在运行,则会抛出异常
PROPAGATION_NESTED表示如果当前已经存在一个事务,那么该方法将会在嵌套事务中运行。嵌套的事务可以独立于当前事务进行单独地提交或回滚。如果当前事务不存在,那么其行为与PROPAGATION_REQUIRED一样。注意各厂商对这种传播行为的支持是有所差异的。可以参考资源管理器的文档来确认它们是否支持嵌套事务


注:以下具体讲解传播行为的内容参考自Spring事务机制详解 
(1)PROPAGATION_REQUIRED 如果存在一个事务,则支持当前事务。如果没有事务则开启一个新的事务。

 
  1. //事务属性 PROPAGATION_REQUIRED

  2. methodA{

  3. ……

  4. methodB();

  5. ……

  6. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
 
  1. //事务属性 PROPAGATION_REQUIRED

  2. methodB{

  3. ……

  4. }

  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务。

单独调用methodB方法:

 
  1. main{

  2. metodB();

  3. }

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

相当于

 
  1. Main{

  2. Connection con=null;

  3. try{

  4. con = getConnection();

  5. con.setAutoCommit(false);

  6.  
  7. //方法调用

  8. methodB();

  9.  
  10. //提交事务

  11. con.commit();

  12. } Catch(RuntimeException ex) {

  13. //回滚事务

  14. con.rollback();

  15. } finally {

  16. //释放资源

  17. closeCon();

  18. }

  19. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Spring保证在methodB方法中所有的调用都获得到一个相同的连接。在调用methodB时,没有一个存在的事务,所以获得一个新的连接,开启了一个新的事务。 
单独调用MethodA时,在MethodA内又会调用MethodB.

执行效果相当于:

 
  1. main{

  2. Connection con = null;

  3. try{

  4. con = getConnection();

  5. methodA();

  6. con.commit();

  7. } catch(RuntimeException ex) {

  8. con.rollback();

  9. } finally {

  10. closeCon();

  11. }

  12. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

调用MethodA时,环境中没有事务,所以开启一个新的事务.当在MethodA中调用MethodB时,环境中已经有了一个事务,所以methodB就加入当前事务。

(2)PROPAGATION_SUPPORTS 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行。但是对于事务同步的事务管理器,PROPAGATION_SUPPORTS与不使用事务有少许不同。

 
  1. //事务属性 PROPAGATION_REQUIRED

  2. methodA(){

  3. methodB();

  4. }

  5.  
  6. //事务属性 PROPAGATION_SUPPORTS

  7. methodB(){

  8. ……

  9. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

单纯的调用methodB时,methodB方法是非事务的执行的。当调用methdA时,methodB则加入了methodA的事务中,事务地执行。

(3)PROPAGATION_MANDATORY 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。

 
  1. //事务属性 PROPAGATION_REQUIRED

  2. methodA(){

  3. methodB();

  4. }

  5.  
  6. //事务属性 PROPAGATION_MANDATORY

  7. methodB(){

  8. ……

  9. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

当单独调用methodB时,因为当前没有一个活动的事务,则会抛出异常throw new IllegalTransactionStateException(“Transaction propagation ‘mandatory’ but no existing transaction found”);当调用methodA时,methodB则加入到methodA的事务中,事务地执行。

(4)PROPAGATION_REQUIRES_NEW 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。

 
  1. //事务属性 PROPAGATION_REQUIRED

  2. methodA(){

  3. doSomeThingA();

  4. methodB();

  5. doSomeThingB();

  6. }

  7.  
  8. //事务属性 PROPAGATION_REQUIRES_NEW

  9. methodB(){

  10. ……

  11. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

调用A方法:

 
  1. main(){

  2. methodA();

  3. }

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

相当于

 
  1. main(){

  2. TransactionManager tm = null;

  3. try{

  4. //获得一个JTA事务管理器

  5. tm = getTransactionManager();

  6. tm.begin();//开启一个新的事务

  7. Transaction ts1 = tm.getTransaction();

  8. doSomeThing();

  9. tm.suspend();//挂起当前事务

  10. try{

  11. tm.begin();//重新开启第二个事务

  12. Transaction ts2 = tm.getTransaction();

  13. methodB();

  14. ts2.commit();//提交第二个事务

  15. } Catch(RunTimeException ex) {

  16. ts2.rollback();//回滚第二个事务

  17. } finally {

  18. //释放资源

  19. }

  20. //methodB执行完后,恢复第一个事务

  21. tm.resume(ts1);

  22. doSomeThingB();

  23. ts1.commit();//提交第一个事务

  24. } catch(RunTimeException ex) {

  25. ts1.rollback();//回滚第一个事务

  26. } finally {

  27. //释放资源

  28. }

  29. }

  • 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
  • 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

在这里,我把ts1称为外层事务,ts2称为内层事务。从上面的代码可以看出,ts2与ts1是两个独立的事务,互不相干。Ts2是否成功并不依赖于 ts1。如果methodA方法在调用methodB方法后的doSomeThingB方法失败了,而methodB方法所做的结果依然被提交。而除了 methodB之外的其它代码导致的结果却被回滚了。使用PROPAGATION_REQUIRES_NEW,需要使用 JtaTransactionManager作为事务管理器。

(5)PROPAGATION_NOT_SUPPORTED 总是非事务地执行,并挂起任何存在的事务。使用PROPAGATION_NOT_SUPPORTED,也需要使用JtaTransactionManager作为事务管理器。(代码示例同上,可同理推出)

(6)PROPAGATION_NEVER 总是非事务地执行,如果存在一个活动事务,则抛出异常。

(7)PROPAGATION_NESTED如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务, 则按TransactionDefinition.PROPAGATION_REQUIRED 属性执行。这是一个嵌套事务,使用JDBC 3.0驱动时,仅仅支持DataSourceTransactionManager作为事务管理器。需要JDBC 驱动的java.sql.Savepoint类。有一些JTA的事务管理器实现可能也提供了同样的功能。使用PROPAGATION_NESTED,还需要把PlatformTransactionManager的nestedTransactionAllowed属性设为true;而 nestedTransactionAllowed属性值默认为false。

 
  1. //事务属性 PROPAGATION_REQUIRED

  2. methodA(){

  3. doSomeThingA();

  4. methodB();

  5. doSomeThingB();

  6. }

  7.  
  8. //事务属性 PROPAGATION_NESTED

  9. methodB(){

  10. ……

  11. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

如果单独调用methodB方法,则按REQUIRED属性执行。如果调用methodA方法,相当于下面的效果:

 
  1. main(){

  2. Connection con = null;

  3. Savepoint savepoint = null;

  4. try{

  5. con = getConnection();

  6. con.setAutoCommit(false);

  7. doSomeThingA();

  8. savepoint = con2.setSavepoint();

  9. try{

  10. methodB();

  11. } catch(RuntimeException ex) {

  12. con.rollback(savepoint);

  13. } finally {

  14. //释放资源

  15. }

  16. doSomeThingB();

  17. con.commit();

  18. } catch(RuntimeException ex) {

  19. con.rollback();

  20. } finally {

  21. //释放资源

  22. }

  23. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

当methodB方法调用之前,调用setSavepoint方法,保存当前的状态到savepoint。如果methodB方法调用失败,则恢复到之前保存的状态。但是需要注意的是,这时的事务并没有进行提交,如果后续的代码(doSomeThingB()方法)调用失败,则回滚包括methodB方法的所有操作。

嵌套事务一个非常重要的概念就是内层事务依赖于外层事务。外层事务失败时,会回滚内层事务所做的动作。而内层事务操作失败并不会引起外层事务的回滚。

PROPAGATION_NESTED 与PROPAGATION_REQUIRES_NEW的区别:它们非常类似,都像一个嵌套事务,如果不存在一个活动的事务,都会开启一个新的事务。使用 PROPAGATION_REQUIRES_NEW时,内层事务与外层事务就像两个独立的事务一样,一旦内层事务进行了提交后,外层事务不能对其进行回滚。两个事务互不影响。两个事务不是一个真正的嵌套事务。同时它需要JTA事务管理器的支持。

使用PROPAGATION_NESTED时,外层事务的回滚可以引起内层事务的回滚。而内层事务的异常并不会导致外层事务的回滚,它是一个真正的嵌套事务。DataSourceTransactionManager使用savepoint支持PROPAGATION_NESTED时,需要JDBC 3.0以上驱动及1.4以上的JDK版本支持。其它的JTA TrasactionManager实现可能有不同的支持方式。

PROPAGATION_REQUIRES_NEW 启动一个新的, 不依赖于环境的 “内部” 事务. 这个事务将被完全 commited 或 rolled back 而不依赖于外部事务, 它拥有自己的隔离范围, 自己的锁, 等等. 当内部事务开始执行时, 外部事务将被挂起, 内务事务结束时, 外部事务将继续执行。

另一方面, PROPAGATION_NESTED 开始一个 “嵌套的” 事务, 它是已经存在事务的一个真正的子事务. 潜套事务开始执行时, 它将取得一个 savepoint. 如果这个嵌套事务失败, 我们将回滚到此 savepoint. 潜套事务是外部事务的一部分, 只有外部事务结束后它才会被提交。

由此可见, PROPAGATION_REQUIRES_NEW 和 PROPAGATION_NESTED 的最大区别在于, PROPAGATION_REQUIRES_NEW 完全是一个新的事务, 而 PROPAGATION_NESTED 则是外部事务的子事务, 如果外部事务 commit, 嵌套事务也会被 commit, 这个规则同样适用于 roll back.

PROPAGATION_REQUIRED应该是我们首先的事务传播行为。它能够满足我们大多数的事务需求。

2.2.2 隔离级别

事务的第二个维度就是隔离级别(isolation level)。隔离级别定义了一个事务可能受其他并发事务影响的程度。 
(1)并发事务引起的问题 
在典型的应用程序中,多个事务并发运行,经常会操作相同的数据来完成各自的任务。并发虽然是必须的,但可能会导致一下的问题。

  • 脏读(Dirty reads)——脏读发生在一个事务读取了另一个事务改写但尚未提交的数据时。如果改写在稍后被回滚了,那么第一个事务获取的数据就是无效的。
  • 不可重复读(Nonrepeatable read)——不可重复读发生在一个事务执行相同的查询两次或两次以上,但是每次都得到不同的数据时。这通常是因为另一个并发事务在两次查询期间进行了更新。
  • 幻读(Phantom read)——幻读与不可重复读类似。它发生在一个事务(T1)读取了几行数据,接着另一个并发事务(T2)插入了一些数据时。在随后的查询中,第一个事务(T1)就会发现多了一些原本不存在的记录。

不可重复读与幻读的区别

不可重复读的重点是修改: 
同样的条件, 你读取过的数据, 再次读取出来发现值不一样了 
例如:在事务1中,Mary 读取了自己的工资为1000,操作并没有完成

 
  1. con1 = getConnection();

  2. select salary from employee empId ="Mary";

  • 1
  • 2
  • 1
  • 2

在事务2中,这时财务人员修改了Mary的工资为2000,并提交了事务.

 
  1. con2 = getConnection();

  2. update employee set salary = 2000;

  3. con2.commit();

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

在事务1中,Mary 再次读取自己的工资时,工资变为了2000

 
  1. //con1

  2. select salary from employee empId ="Mary";

  • 1
  • 2
  • 1
  • 2

在一个事务中前后两次读取的结果并不一致,导致了不可重复读。

幻读的重点在于新增或者删除: 
同样的条件, 第1次和第2次读出来的记录数不一样 
例如:目前工资为1000的员工有10人。事务1,读取所有工资为1000的员工。

 
  1. con1 = getConnection();

  2. Select * from employee where salary =1000;

  • 1
  • 2
  • 1
  • 2

共读取10条记录

这时另一个事务向employee表插入了一条员工记录,工资也为1000

 
  1. con2 = getConnection();

  2. Insert into employee(empId,salary) values("Lili",1000);

  3. con2.commit();

  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

事务1再次读取所有工资为1000的员工

 
  1. //con1

  2. select * from employee where salary =1000;

  • 1
  • 2
  • 1
  • 2

共读取到了11条记录,这就产生了幻像读。

从总的结果来看, 似乎不可重复读和幻读都表现为两次读取的结果不一致。但如果你从控制的角度来看, 两者的区别就比较大。 
对于前者, 只需要锁住满足条件的记录。 
对于后者, 要锁住满足条件及其相近的记录。

(2)隔离级别

隔离级别含义
ISOLATION_DEFAULT使用后端数据库默认的隔离级别
ISOLATION_READ_UNCOMMITTED最低的隔离级别,允许读取尚未提交的数据变更,可能会导致脏读、幻读或不可重复读
ISOLATION_READ_COMMITTED允许读取并发事务已经提交的数据,可以阻止脏读,但是幻读或不可重复读仍有可能发生
ISOLATION_REPEATABLE_READ对同一字段的多次读取结果都是一致的,除非数据是被本身事务自己所修改,可以阻止脏读和不可重复读,但幻读仍有可能发生
ISOLATION_SERIALIZABLE最高的隔离级别,完全服从ACID的隔离级别,确保阻止脏读、不可重复读以及幻读,也是最慢的事务隔离级别,因为它通常是通过完全锁定事务相关的数据库表来实现的

2.2.3 只读

事务的第三个特性是它是否为只读事务。如果事务只对后端的数据库进行该操作,数据库可以利用事务的只读特性来进行一些特定的优化。通过将事务设置为只读,你就可以给数据库一个机会,让它应用它认为合适的优化措施。

2.2.4 事务超时

为了使应用程序很好地运行,事务不能运行太长的时间。因为事务可能涉及对后端数据库的锁定,所以长时间的事务会不必要的占用数据库资源。事务超时就是事务的一个定时器,在特定时间内事务如果没有执行完毕,那么就会自动回滚,而不是一直等待其结束。

2.2.5 回滚规则

事务五边形的最后一个方面是一组规则,这些规则定义了哪些异常会导致事务回滚而哪些不会。默认情况下,事务只有遇到运行期异常时才会回滚,而在遇到检查型异常时不会回滚(这一行为与EJB的回滚行为是一致的) 
但是你可以声明事务在遇到特定的检查型异常时像遇到运行期异常那样回滚。同样,你还可以声明事务遇到特定的异常不回滚,即使这些异常是运行期异常。

2.3 事务状态

上面讲到的调用PlatformTransactionManager接口的getTransaction()的方法得到的是TransactionStatus接口的一个实现,这个接口的内容如下:

 
  1. public interface TransactionStatus{

  2. boolean isNewTransaction(); // 是否是新的事物

  3. boolean hasSavepoint(); // 是否有恢复点

  4. void setRollbackOnly(); // 设置为只回滚

  5. boolean isRollbackOnly(); // 是否为只回滚

  6. boolean isCompleted; // 是否已完成

  7. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

可以发现这个接口描述的是一些处理事务提供简单的控制事务执行和查询事务状态的方法,在回滚或提交的时候需要应用对应的事务状态。

3 编程式事务

3.1 编程式和声明式事务的区别

Spring提供了对编程式事务和声明式事务的支持,编程式事务允许用户在代码中精确定义事务的边界,而声明式事务(基于AOP)有助于用户将操作与事务规则进行解耦。 
简单地说,编程式事务侵入到了业务代码里面,但是提供了更加详细的事务管理;而声明式事务由于基于AOP,所以既能起到事务管理的作用,又可以不影响业务代码的具体实现。

3.2 如何实现编程式事务?

Spring提供两种方式的编程式事务管理,分别是:使用TransactionTemplate和直接使用PlatformTransactionManager。

3.2.1 使用TransactionTemplate

采用TransactionTemplate和采用其他Spring模板,如JdbcTempalte和HibernateTemplate是一样的方法。它使用回调方法,把应用程序从处理取得和释放资源中解脱出来。如同其他模板,TransactionTemplate是线程安全的。代码片段:

 
  1. TransactionTemplate tt = new TransactionTemplate(); // 新建一个TransactionTemplate

  2. Object result = tt.execute(

  3. new TransactionCallback(){

  4. public Object doTransaction(TransactionStatus status){

  5. updateOperation();

  6. return resultOfUpdateOperation();

  7. }

  8. }); // 执行execute方法进行事务管理

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

使用TransactionCallback()可以返回一个值。如果使用TransactionCallbackWithoutResult则没有返回值。

3.2.2 使用PlatformTransactionManager

示例代码如下:

 
  1.  
  2. DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); //定义一个某个框架平台的TransactionManager,如JDBC、Hibernate

  3. dataSourceTransactionManager.setDataSource(this.getJdbcTemplate().getDataSource()); // 设置数据源

  4. DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); // 定义事务属性

  5. transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED); // 设置传播行为属性

  6. TransactionStatus status = dataSourceTransactionManager.getTransaction(transDef); // 获得事务状态

  7. try {

  8. // 数据库操作

  9. dataSourceTransactionManager.commit(status);// 提交

  10. } catch (Exception e) {

  11. dataSourceTransactionManager.rollback(status);// 回滚

  12. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

4 声明式事务

4.1 配置方式

注:以下配置代码参考自Spring事务配置的五种方式

根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:

(1)每个Bean都有一个代理

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  8. http://www.springframework.org/schema/context

  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  11.  
  12. <bean id="sessionFactory"

  13. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  14. <property name="configLocation" value="classpath:hibernate.cfg.xml" />

  15. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

  16. </bean>

  17.  
  18. <!-- 定义事务管理器(声明式的事务) -->

  19. <bean id="transactionManager"

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

  21. <property name="sessionFactory" ref="sessionFactory" />

  22. </bean>

  23.  
  24. <!-- 配置DAO -->

  25. <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">

  26. <property name="sessionFactory" ref="sessionFactory" />

  27. </bean>

  28.  
  29. <bean id="userDao"

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

  31. <!-- 配置事务管理器 -->

  32. <property name="transactionManager" ref="transactionManager" />

  33. <property name="target" ref="userDaoTarget" />

  34. <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />

  35. <!-- 配置事务属性 -->

  36. <property name="transactionAttributes">

  37. <props>

  38. <prop key="*">PROPAGATION_REQUIRED</prop>

  39. </props>

  40. </property>

  41. </bean>

  42. </beans>

  • 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
  • 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

(2)所有Bean共享一个代理基类

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  8. http://www.springframework.org/schema/context

  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  11.  
  12. <bean id="sessionFactory"

  13. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  14. <property name="configLocation" value="classpath:hibernate.cfg.xml" />

  15. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

  16. </bean>

  17.  
  18. <!-- 定义事务管理器(声明式的事务) -->

  19. <bean id="transactionManager"

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

  21. <property name="sessionFactory" ref="sessionFactory" />

  22. </bean>

  23.  
  24. <bean id="transactionBase"

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

  26. lazy-init="true" abstract="true">

  27. <!-- 配置事务管理器 -->

  28. <property name="transactionManager" ref="transactionManager" />

  29. <!-- 配置事务属性 -->

  30. <property name="transactionAttributes">

  31. <props>

  32. <prop key="*">PROPAGATION_REQUIRED</prop>

  33. </props>

  34. </property>

  35. </bean>

  36.  
  37. <!-- 配置DAO -->

  38. <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">

  39. <property name="sessionFactory" ref="sessionFactory" />

  40. </bean>

  41.  
  42. <bean id="userDao" parent="transactionBase" >

  43. <property name="target" ref="userDaoTarget" />

  44. </bean>

  45. </beans>

  • 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
  • 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

(3)使用拦截器

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  8. http://www.springframework.org/schema/context

  9. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

  11.  
  12. <bean id="sessionFactory"

  13. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  14. <property name="configLocation" value="classpath:hibernate.cfg.xml" />

  15. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

  16. </bean>

  17.  
  18. <!-- 定义事务管理器(声明式的事务) -->

  19. <bean id="transactionManager"

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

  21. <property name="sessionFactory" ref="sessionFactory" />

  22. </bean>

  23.  
  24. <bean id="transactionInterceptor"

  25. class="org.springframework.transaction.interceptor.TransactionInterceptor">

  26. <property name="transactionManager" ref="transactionManager" />

  27. <!-- 配置事务属性 -->

  28. <property name="transactionAttributes">

  29. <props>

  30. <prop key="*">PROPAGATION_REQUIRED</prop>

  31. </props>

  32. </property>

  33. </bean>

  34.  
  35. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

  36. <property name="beanNames">

  37. <list>

  38. <value>*Dao</value>

  39. </list>

  40. </property>

  41. <property name="interceptorNames">

  42. <list>

  43. <value>transactionInterceptor</value>

  44. </list>

  45. </property>

  46. </bean>

  47.  
  48. <!-- 配置DAO -->

  49. <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">

  50. <property name="sessionFactory" ref="sessionFactory" />

  51. </bean>

  52. </beans>

  • 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
  • 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

(4)使用tx标签配置的拦截器

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xmlns:tx="http://www.springframework.org/schema/tx"

  7. xsi:schemaLocation="http://www.springframework.org/schema/beans

  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  9. http://www.springframework.org/schema/context

  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

  13.  
  14. <context:annotation-config />

  15. <context:component-scan base-package="com.bluesky" />

  16.  
  17. <bean id="sessionFactory"

  18. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  19. <property name="configLocation" value="classpath:hibernate.cfg.xml" />

  20. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

  21. </bean>

  22.  
  23. <!-- 定义事务管理器(声明式的事务) -->

  24. <bean id="transactionManager"

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

  26. <property name="sessionFactory" ref="sessionFactory" />

  27. </bean>

  28.  
  29. <tx:advice id="txAdvice" transaction-manager="transactionManager">

  30. <tx:attributes>

  31. <tx:method name="*" propagation="REQUIRED" />

  32. </tx:attributes>

  33. </tx:advice>

  34.  
  35. <aop:config>

  36. <aop:pointcut id="interceptorPointCuts"

  37. expression="execution(* com.bluesky.spring.dao.*.*(..))" />

  38. <aop:advisor advice-ref="txAdvice"

  39. pointcut-ref="interceptorPointCuts" />

  40. </aop:config>

  41. </beans>

  • 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
  • 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

(5)全注解

 
  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4. xmlns:context="http://www.springframework.org/schema/context"

  5. xmlns:aop="http://www.springframework.org/schema/aop"

  6. xmlns:tx="http://www.springframework.org/schema/tx"

  7. xsi:schemaLocation="http://www.springframework.org/schema/beans

  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  9. http://www.springframework.org/schema/context

  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd

  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

  13.  
  14. <context:annotation-config />

  15. <context:component-scan base-package="com.bluesky" />

  16.  
  17. <tx:annotation-driven transaction-manager="transactionManager"/>

  18.  
  19. <bean id="sessionFactory"

  20. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

  21. <property name="configLocation" value="classpath:hibernate.cfg.xml" />

  22. <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />

  23. </bean>

  24.  
  25. <!-- 定义事务管理器(声明式的事务) -->

  26. <bean id="transactionManager"

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

  28. <property name="sessionFactory" ref="sessionFactory" />

  29. </bean>

  30.  
  31. </beans>

  • 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
  • 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

此时在DAO上需加上@Transactional注解,如下:

 
  1. package com.bluesky.spring.dao;

  2.  
  3. import java.util.List;

  4.  
  5. import org.hibernate.SessionFactory;

  6. import org.springframework.beans.factory.annotation.Autowired;

  7. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

  8. import org.springframework.stereotype.Component;

  9.  
  10. import com.bluesky.spring.domain.User;

  11.  
  12. @Transactional

  13. @Component("userDao")

  14. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

  15.  
  16. public List<User> listUsers() {

  17. return this.getSession().createQuery("from User").list();

  18. }

  19. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

4.2 一个声明式事务的实例

注:该实例参考自Spring中的事务管理实例详解

首先是数据库表 
book(isbn, book_name, price) 
account(username, balance) 
book_stock(isbn, stock)

然后是XML配置

 
  1. <beans xmlns="http://www.springframework.org/schema/beans"

  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xmlns:context="http://www.springframework.org/schema/context"

  4. xmlns:aop="http://www.springframework.org/schema/aop"

  5. xmlns:tx="http://www.springframework.org/schema/tx"

  6. xsi:schemaLocation="http://www.springframework.org/schema/beans

  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

  8. http://www.springframework.org/schema/context

  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd

  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

  11. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

  12.  
  13. <import resource="applicationContext-db.xml" />

  14.  
  15. <context:component-scan

  16. base-package="com.springinaction.transaction">

  17. </context:component-scan>

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

  20.  
  21. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  22. <property name="dataSource" ref="dataSource" />

  23. </bean>

  24.  
  25. </beans>

  • 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
  • 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

使用的类 
BookShopDao

 
  1. package com.springinaction.transaction;

  2.  
  3. public interface BookShopDao {

  4. // 根据书号获取书的单价

  5. public int findBookPriceByIsbn(String isbn);

  6. // 更新书的库存,使书号对应的库存-1

  7. public void updateBookStock(String isbn);

  8. // 更新用户的账户余额:account的balance-price

  9. public void updateUserAccount(String username, int price);

  10. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

BookShopDaoImpl

 
  1. package com.springinaction.transaction;

  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.jdbc.core.JdbcTemplate;

  5. import org.springframework.stereotype.Repository;

  6.  
  7. @Repository("bookShopDao")

  8. public class BookShopDaoImpl implements BookShopDao {

  9.  
  10. @Autowired

  11. private JdbcTemplate JdbcTemplate;

  12.  
  13. @Override

  14. public int findBookPriceByIsbn(String isbn) {

  15. String sql = "SELECT price FROM book WHERE isbn = ?";

  16.  
  17. return JdbcTemplate.queryForObject(sql, Integer.class, isbn);

  18. }

  19.  
  20. @Override

  21. public void updateBookStock(String isbn) {

  22. //检查书的库存是否足够,若不够,则抛出异常

  23. String sql2 = "SELECT stock FROM book_stock WHERE isbn = ?";

  24. int stock = JdbcTemplate.queryForObject(sql2, Integer.class, isbn);

  25. if (stock == 0) {

  26. throw new BookStockException("库存不足!");

  27. }

  28. String sql = "UPDATE book_stock SET stock = stock - 1 WHERE isbn = ?";

  29. JdbcTemplate.update(sql, isbn);

  30. }

  31.  
  32. @Override

  33. public void updateUserAccount(String username, int price) {

  34. //检查余额是否不足,若不足,则抛出异常

  35. String sql2 = "SELECT balance FROM account WHERE username = ?";

  36. int balance = JdbcTemplate.queryForObject(sql2, Integer.class, username);

  37. if (balance < price) {

  38. throw new UserAccountException("余额不足!");

  39. }

  40. String sql = "UPDATE account SET balance = balance - ? WHERE username = ?";

  41. JdbcTemplate.update(sql, price, username);

  42. }

  43.  
  44. }

  • 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
  • 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

BookShopService

 
  1. package com.springinaction.transaction;

  2. public interface BookShopService {

  3. public void purchase(String username, String isbn);

  4. }

  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

BookShopServiceImpl

 
  1. package com.springinaction.transaction;

  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;

  4. import org.springframework.stereotype.Service;

  5. import org.springframework.transaction.annotation.Isolation;

  6. import org.springframework.transaction.annotation.Propagation;

  7. import org.springframework.transaction.annotation.Transactional;

  8.  
  9. @Service("bookShopService")

  10. public class BookShopServiceImpl implements BookShopService {

  11.  
  12. @Autowired

  13. private BookShopDao bookShopDao;

  14.  
  15. /**

  16. * 1.添加事务注解

  17. * 使用propagation 指定事务的传播行为,即当前的事务方法被另外一个事务方法调用时如何使用事务。

  18. * 默认取值为REQUIRED,即使用调用方法的事务

  19. * REQUIRES_NEW:使用自己的事务,调用的事务方法的事务被挂起。

  20. *

  21. * 2.使用isolation 指定事务的隔离级别,最常用的取值为READ_COMMITTED

  22. * 3.默认情况下 Spring 的声明式事务对所有的运行时异常进行回滚,也可以通过对应的属性进行设置。通常情况下,默认值即可。

  23. * 4.使用readOnly 指定事务是否为只读。 表示这个事务只读取数据但不更新数据,这样可以帮助数据库引擎优化事务。若真的是一个只读取数据库值得方法,应设置readOnly=true

  24. * 5.使用timeOut 指定强制回滚之前事务可以占用的时间。

  25. */

  26. @Transactional(propagation=Propagation.REQUIRES_NEW,

  27. isolation=Isolation.READ_COMMITTED,

  28. noRollbackFor={UserAccountException.class},

  29. readOnly=true, timeout=3)

  30. @Override

  31. public void purchase(String username, String isbn) {

  32. //1.获取书的单价

  33. int price = bookShopDao.findBookPriceByIsbn(isbn);

  34. //2.更新书的库存

  35. bookShopDao.updateBookStock(isbn);

  36. //3.更新用户余额

  37. bookShopDao.updateUserAccount(username, price);

  38. }

  39. }

  • 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
  • 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

Cashier

 
  1. package com.springinaction.transaction;

  2. import java.util.List;

  3. public interface Cashier {

  4. public void checkout(String username, List<String>isbns);

  5. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

CashierImpl:CashierImpl.checkout和bookShopService.purchase联合测试了事务的传播行为

 
  1. package com.springinaction.transaction;

  2.  
  3. import java.util.List;

  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;

  6. import org.springframework.stereotype.Service;

  7. import org.springframework.transaction.annotation.Transactional;

  8.  
  9. @Service("cashier")

  10. public class CashierImpl implements Cashier {

  11. @Autowired

  12. private BookShopService bookShopService;

  13.  
  14. @Transactional

  15. @Override

  16. public void checkout(String username, List<String> isbns) {

  17. for(String isbn : isbns) {

  18. bookShopService.purchase(username, isbn);

  19. }

  20. }

  21. }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

BookStockException

 
  1.  
  2. package com.springinaction.transaction;

  3. public class BookStockException extends RuntimeException {

  4.  
  5. private static final long serialVersionUID = 1L;

  6.  
  7. public BookStockException() {

  8. super();

  9. // TODO Auto-generated constructor stub

  10. }

  11.  
  12. public BookStockException(String arg0, Throwable arg1, boolean arg2,

  13. boolean arg3) {

  14. super(arg0, arg1, arg2, arg3);

  15. // TODO Auto-generated constructor stub

  16. }

  17.  
  18. public BookStockException(String arg0, Throwable arg1) {

  19. super(arg0, arg1);

  20. // TODO Auto-generated constructor stub

  21. }

  22.  
  23. public BookStockException(String arg0) {

  24. super(arg0);

  25. // TODO Auto-generated constructor stub

  26. }

  27.  
  28. public BookStockException(Throwable arg0) {

  29. super(arg0);

  30. // TODO Auto-generated constructor stub

  31. }

  32. }

  • 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
  • 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

UserAccountException

 
  1. package com.springinaction.transaction;

  2. public class UserAccountException extends RuntimeException {

  3.  
  4. private static final long serialVersionUID = 1L;

  5.  
  6. public UserAccountException() {

  7. super();

  8. // TODO Auto-generated constructor stub

  9. }

  10.  
  11. public UserAccountException(String arg0, Throwable arg1, boolean arg2,

  12. boolean arg3) {

  13. super(arg0, arg1, arg2, arg3);

  14. // TODO Auto-generated constructor stub

  15. }

  16.  
  17. public UserAccountException(String arg0, Throwable arg1) {

  18. super(arg0, arg1);

  19. // TODO Auto-generated constructor stub

  20. }

  21.  
  22. public UserAccountException(String arg0) {

  23. super(arg0);

  24. // TODO Auto-generated constructor stub

  25. }

  26.  
  27. public UserAccountException(Throwable arg0) {

  28. super(arg0);

  29. // TODO Auto-generated constructor stub

  30. }

  31. }

  • 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
  • 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

测试类

 
  1. package com.springinaction.transaction;

  2.  
  3. import java.util.Arrays;

  4.  
  5. import org.junit.Test;

  6. import org.springframework.context.ApplicationContext;

  7. import org.springframework.context.support.ClassPathXmlApplicationContext;

  8.  
  9. public class SpringTransitionTest {

  10.  
  11. private ApplicationContext ctx = null;

  12. private BookShopDao bookShopDao = null;

  13. private BookShopService bookShopService = null;

  14. private Cashier cashier = null;

  15. {

  16. ctx = new ClassPathXmlApplicationContext("config/transaction.xml");

  17. bookShopDao = ctx.getBean(BookShopDao.class);

  18. bookShopService = ctx.getBean(BookShopService.class);

  19. cashier = ctx.getBean(Cashier.class);

  20. }

  21.  
  22. @Test

  23. public void testBookShopDaoFindPriceByIsbn() {

  24. System.out.println(bookShopDao.findBookPriceByIsbn("1001"));

  25. }

  26.  
  27. @Test

  28. public void testBookShopDaoUpdateBookStock(){

  29. bookShopDao.updateBookStock("1001");

  30. }

  31.  
  32. @Test

  33. public void testBookShopDaoUpdateUserAccount(){

  34. bookShopDao.updateUserAccount("AA", 100);

  35. }

  36. @Test

  37. public void testBookShopService(){

  38. bookShopService.purchase("AA", "1001");

  39. }

  40.  
  41. @Test

  42. public void testTransactionPropagation(){

  43. cashier.checkout("AA", Arrays.asList("1001", "1002"));

  44. }

  45. }

本文转自:http://blog.csdn.net/trigl/article/details/50968079

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值