Spring事务管理的四种方式(以银行转账为例)
一、事务的作用
将若干的数据库操作作为一个整体控制,一起成功或一起失败。
原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生。
一致性:指事务前后数据的完整性必须保持一致。
隔离性:指多个用户并发访问数据库时,一个用户的事务不能被其他用户的事务所干扰,多个并发事务之间数据要相互隔离。
持久性:指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,即时数据库发生故障也不应该对其有任何影响。
二、Spring事务管理高层抽象主要包括3个接口
--Platform TransactionManager 事务管理器(提交、回滚事务)
Spring为不同的持久化框架提供了不同的Platform
TransactionManager接口实现。如:
使用Spring
JDBC或iBatis进行持久化数据时使用DataSourceTransactionManager
使用Hibernate3.0版本进行持久化数据时使用HibernateTransactionManager
--TransactionDefinition 事务定义信息(隔离、传播、超时、只读)
脏读:一个事务读取了另一个事务改写但还未提交的数据,如果这些数据被回滚,则读到的数据是无效的。
不可重复读:在同一事务中,多次读取同一数据返回的结果有所不同。
幻读:一个事务读取了几行记录后,另一个事务插入一些记录,幻读就发生了。再后来的查询中,第一个事务就会发现有些原来没有的记录。
事务隔离级别:(五种)
- DEFAULT--使用后端数据库默认的隔离级别(Spring中的选择项)
- READ_UNCOMMITED--允许你读取还未提交的改变了的数据。可能导致脏、幻、不可重复读
- READ_COMMITTED--允许在并发事务已经提交后读取。可防止脏读,但幻读和不可重复读仍可发生
- REPEATABLE_READ--对相同字段的多次读取是一致的,除非数据被事务本身改变。可防止脏、不可重复读,但幻读仍可能发生
- SERIALIZABLE--完全服从ACID的隔离级别,确保不发生脏、幻、不可重复读。这在所有的隔离级别中是最慢的,它是典型的通过完全锁定在事务中涉及的数据表来完成的
其中,MySQL默认采用REPEATABLE_READ隔离级别;Oracle默认采用READ_COMMITTED隔离级别
事务传播行为:(七种)
- REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
- SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
- MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
- REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
- NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
- NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
- NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与REQUIRED类似的操作。拥有多个可以回滚的保存点,内部回滚不会对外部事务产生影响。只对DataSourceTransactionManager有效
--TransactionStatus 事务具体运行状态
三、Spring提供了以下方法控制事务
a.编程式事务管理(基于Java编程控制,很少使用)--见demo1包
利用TransactionTemplate将多个DAO操作封装起来
*b.声明式事务管理(基于Spring的AOP配置控制)
-基于TransactionProxyFactoryBean的方式.(很少使用)--见demo2包
需要为每个进行事务管理的类,配置一个TransactionProxyFactoryBean进行增强.
-基于XML配置(经常使用)--见demo3包
一旦配置好之后,类上不需要添加任何东西。
如果Action作为目标对象切入事务,需要在<aop:config>元素里添加proxy-target-class="true"属性。原因是通知Spring框架采用CGLIB技术生成具有事务管理功能的Action类。
-基于注解(配置简单,经常使用)--见demo4包
在applicationContext.xml中开启事务注解配置。(applicationContext.xml中只需定义Bean并追加以下元素)
<bean id="txManager" class="...">
<property name="sessionFactory">
</property>
<tx:annotation-driven transaction-manager="txManager"/>
在目标组件类中使用@Transactional,该标记可定义在类前或方法前。
四、示例(银行转账)
--编程式
[java] view
plain copy
- /**
- * @Description:转账案例的DAO层接口
- *
- */
- public interface AccountDao {
- /**
- * @param out
- * :转出账号
- * @param money
- 10. * :转账金额
- 11. */
- 12. public void outMoney(String out, Double money);
- 13.
- 14. /**
- 15. *
- 16. * @param in
- 17. * :转入账号
- 18. * @param money
- 19. * :转账金额
- 20. */
- 21. public void inMoney(String in, Double money);
22. }
[java] view
plain copy
- /**
- * @Description:转账案例的DAO层实现类
- */
- public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
- /**
- * @param out
- * :转出账号
- * @param money
- * :转账金额
- 10. */
- 11. @Override
- 12. public void outMoney(String out, Double money) {
- 13. String sql = "update account set money = money-? where name = ?";
- 14. this.getJdbcTemplate().update(sql, money, out);
- 15. }
- 16. /**
- 17. * @param in
- 18. * :转入账号
- 19. * @param money
- 20. * :转账金额
- 21. */
- 22. @Override
- 23. public void inMoney(String in, Double money) {
- 24. String sql = "update account set money = money+? where name = ?";
- 25. this.getJdbcTemplate().update(sql, money, in);
- 26. }
27. }
[java] view
plain copy
- /**
- * @Description:转账案例的业务接口
- *
- */
- public interface AccountService {
- /**
- * @param out :转出账号
- * @param in :转入账号
- * @param money :转账金额
- 10. */
- 11. public void transfer(String out,String in,Double money);
12. }
[java] view
plain copy
- /**
- * @Description:转账案例的业务层实现类
- */
- public class AccountServiceImpl implements AccountService {
- // 注入转账的DAO
- private AccountDao accountDao;
- // 注入事务管理的模板
- private TransactionTemplate transactionTemplate;
- 10.
- 11. /**
- 12. * @param out
- 13. * :转出账号
- 14. * @param in
- 15. * :转入账号
- 16. * @param money
- 17. * :转账金额
- 18. */
- 19. @Override
- 20. public void transfer(final String out, final String in, final Double money) {
- 21.
- 22. // 未经事务控制的业务处理操作,如果过程中出异常,则导致前面的操作能完成,后面的不能,即转账成功但未收到转账款
- 23. // accountDao.outMoney(out, money);
- 24. // int i = 1/0;
- 25. // accountDao.inMoney(in, money);
- 26.
- 27. transactionTemplate.execute(new TransactionCallbackWithoutResult() {
- 28.
- 29. @Override
- 30. protected void doInTransactionWithoutResult(
- 31. TransactionStatus transactionStatus) {
- 32. accountDao.outMoney(out, money);
- 33. // int i = 1 / 0;//事务控制,即出现异常,该段内代码都执行失效
- 34. accountDao.inMoney(in, money);
- 35. }
- 36. });
- 37. }
- 38.
- 39. public void setAccountDao(AccountDao accountDao) {
- 40. this.accountDao = accountDao;
- 41. }
- 42.
- 43. public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
- 44. this.transactionTemplate = transactionTemplate;
- 45. }
46. }
applicationContext1.xml
[html] view
plain copy
- <!-- 引入外部的属性文件 -->
- <context:property-placeholder location="classpath:jdbc.properties"/>
- <!-- 配置c3p0连接池 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverClass}" />
- <property name="jdbcUrl" value="${jdbc.url}" />
- <property name="user" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- 10. </bean>
- 11.
- 12. <!-- 配置业务层类 -->
- 13. <bean id="accountService" class="com.zs.spring.demo1.AccountServiceImpl">
- 14. <property name="accountDao" ref="accountDao" />
- 15. <!-- 注入事务管理的模板 -->
- 16. <property name="transactionTemplate" ref="transactionTemplate" />
- 17. </bean>
- 18.
- 19. <!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->
- 20. <bean id="accountDao" class="com.zs.spring.demo1.AccountDaoImpl">
- 21. <property name="dataSource" ref="dataSource" />
- 22. </bean>
- 23.
- 24. <!-- 配置DAO类(未简化) -->
- 25. <!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- 26. <property name="dataSource" ref="dataSource" />
- 27. </bean>
- 28. <bean id="accountDao" class="com.zs.spring.demo1.AccountDaoImpl">
- 29. <property name="jdbcTemplate" ref="jdbcTemplate" />
- 30. </bean> -->
- 31.
- 32. <!-- ==================================1.编程式的事务管理=============================================== -->
- 33. <!-- 配置事务管理器 -->
- 34. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- 35. <property name="dataSource" ref="dataSource" />
- 36. </bean>
- 37.
- 38. <!-- 配置事务管理的模板:Spring为了简化事务管理的代码而提供的类 -->
- 39. <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
- 40. <property name="transactionManager" ref="transactionManager"/>
- 41. </bean>
测试:
[java] view
plain copy
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:applicationContext1.xml")
- public class TransactionTest {
- @Resource(name = "accountService")
- private AccountService accountService;
- @Test
- public void demo1() {
- accountService.transfer("aaa", "bbb", 200d);
- 10. }
11. }
--基于TransactionProxyFactoryBean的方式
[java] view
plain copy
- public class AccountServiceImpl implements AccountService {
- // 注入转账的DAO
- private AccountDao accountDao;
- /**
- * @param out
- * :转出账号
- * @param in
- * :转入账号
- 10. * @param money
- 11. * :转账金额
- 12. */
- 13. @Override
- 14. public void transfer(String out, String in, Double money) {
- 15. accountDao.outMoney(out, money);
- 16. // int i = 1/0;
- 17. accountDao.inMoney(in, money);
- 18. }
- 19.
- 20. public void setAccountDao(AccountDao accountDao) {
- 21. this.accountDao = accountDao;
- 22. }
23. }
applicationContext2.xml
[html] view
plain copy
- <!-- 引入外部的属性文件 -->
- <context:property-placeholder location="classpath:jdbc.properties"/>
- <!-- 配置c3p0连接池 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverClass}" />
- <property name="jdbcUrl" value="${jdbc.url}" />
- <property name="user" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- 10. </bean>
- 11.
- 12. <!-- 配置业务层类 -->
- 13. <bean id="accountService" class="com.zs.spring.demo2.AccountServiceImpl">
- 14. <property name="accountDao" ref="accountDao" />
- 15. </bean>
- 16.
- 17. <!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->
- 18. <bean id="accountDao" class="com.zs.spring.demo2.AccountDaoImpl">
- 19. <property name="dataSource" ref="dataSource" />
- 20. </bean>
- 21.
- 22. <!-- ==================================2.使用XML配置声明式的事务管理(原始方式)=============================================== -->
- 23.
- 24. <!-- 配置事务管理器 -->
- 25. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- 26. <property name="dataSource" ref="dataSource" />
- 27. </bean>
- 28.
- 29. <!-- 配置业务层的代理 -->
- 30. <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- 31. <!-- 配置目标对象 -->
- 32. <property name="target" ref="accountService" />
- 33. <!-- 注入事务管理器 -->
- 34. <property name="transactionManager" ref="transactionManager"></property>
- 35. <!-- 注入事务的属性 -->
- 36. <property name="transactionAttributes">
- 37. <props>
- 38. <!--
- 39. prop的格式:
- 40. * PROPAGATION :事务的传播行为
- 41. * ISOTATION :事务的隔离级别
- 42. * readOnly :只读
- 43. * -EXCEPTION :发生哪些异常回滚事务
- 44. * +EXCEPTION :发生哪些异常不回滚事务
- 45. -->
- 46. <prop key="transfer">PROPAGATION_REQUIRED</prop>
- 47. <!-- <prop key="transfer">PROPAGATION_REQUIRED,readOnly</prop> -->
- 48. <!-- <prop key="transfer">PROPAGATION_REQUIRED,+java.lang.ArithmeticException</prop> -->
- 49. </props>
- 50. </property>
- 51. </bean>
测试:
[java] view
plain copy
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:applicationContext2.xml")
- public class TransactionTest {
- /**
- * 一定要注入代理类:因为代理类进行增强的操作
- */
- // @Resource(name="accountService")
- @Resource(name = "accountServiceProxy")
- private AccountService accountService;
- 10.
- 11. @Test
- 12. public void demo1() {
- 13. accountService.transfer("aaa", "bbb", 200d);
- 14. }
15. }
--基于XML配置
[java] view
plain copy
- public class AccountServiceImpl implements AccountService {
- // 注入转账的DAO
- private AccountDao accountDao;
- /**
- * @param out
- * :转出账号
- * @param in
- * :转入账号
- 10. * @param money
- 11. * :转账金额
- 12. */
- 13. @Override
- 14. public void transfer(String out, String in, Double money) {
- 15. accountDao.outMoney(out, money);
- 16. // int i = 1/0;
- 17. accountDao.inMoney(in, money);
- 18.
- 19. }
- 20.
- 21. public void setAccountDao(AccountDao accountDao) {
- 22. this.accountDao = accountDao;
- 23. }
24. }
applicationContext3.xml
[html] view
plain copy
- <!-- 引入外部的属性文件 -->
- <context:property-placeholder location="classpath:jdbc.properties"/>
- <!-- 配置c3p0连接池 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverClass}" />
- <property name="jdbcUrl" value="${jdbc.url}" />
- <property name="user" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- 10. </bean>
- 11.
- 12. <!-- 配置业务层类 -->
- 13. <bean id="accountService" class="com.zs.spring.demo3.AccountServiceImpl">
- 14. <property name="accountDao" ref="accountDao" />
- 15. </bean>
- 16.
- 17. <!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->
- 18. <bean id="accountDao" class="com.zs.spring.demo3.AccountDaoImpl">
- 19. <property name="dataSource" ref="dataSource" />
- 20. </bean>
- 21.
- 22. <!-- ==================================3.使用XML配置声明式的事务管理,基于tx/aop=============================================== -->
- 23.
- 24. <!-- 配置事务管理器 -->
- 25. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- 26. <property name="dataSource" ref="dataSource" />
- 27. </bean>
- 28.
- 29. <!-- 配置事务的通知 -->
- 30. <tx:advice id="txAdvice" transaction-manager="transactionManager">
- 31. <tx:attributes>
- 32. <!--
- 33. propagation :事务传播行为
- 34. isolation :事务的隔离级别
- 35. read-only :只读
- 36. rollback-for:发生哪些异常回滚
- 37. no-rollback-for :发生哪些异常不回滚
- 38. timeout :过期信息
- 39. -->
- 40. <tx:method name="transfer" propagation="REQUIRED"/>
- 41. </tx:attributes>
- 42. </tx:advice>
- 43.
- 44. <!-- 配置切面 -->
- 45. <aop:config>
- 46. <!-- 配置切入点 -->
- 47. <aop:pointcut expression="execution(* com.zs.spring.demo3.AccountService+.*(..))" id="pointcut1"/>
- 48. <!-- 配置切面 -->
- 49. <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
- 50. </aop:config>
测试:
[java] view
plain copy
- /**
- * @Description:Spring的声明式事务管理的方式二:基于AspectJ的XML方式的配置
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:applicationContext3.xml")
- public class TransactionTest {
- /**
- * 一定要注入代理类:因为代理类进行增强的操作
- */
- 10. @Resource(name = "accountService")
- 11. private AccountService accountService;
- 12.
- 13. @Test
- 14. public void demo1() {
- 15. accountService.transfer("aaa", "bbb", 200d);
- 16. }
17. }
--基于注解
[java] view
plain copy
- /**
- * @Transactional中的的属性 propagation :事务的传播行为 isolation :事务的隔离级别 readOnly :只读
- * rollbackFor :发生哪些异常回滚 noRollbackFor :发生哪些异常不回滚
- * rollbackForClassName 根据异常类名回滚
- */
- @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = false)
- public class AccountServiceImpl implements AccountService {
- // 注入转账的DAO
- private AccountDao accountDao;
- 10.
- 11. /**
- 12. * @param out
- 13. * :转出账号
- 14. * @param in
- 15. * :转入账号
- 16. * @param money
- 17. * :转账金额
- 18. */
- 19. @Override
- 20. public void transfer(String out, String in, Double money) {
- 21. accountDao.outMoney(out, money);
- 22. // int i = 1/0;
- 23. accountDao.inMoney(in, money);
- 24. }
- 25.
- 26. public void setAccountDao(AccountDao accountDao) {
- 27. this.accountDao = accountDao;
- 28. }
29. }
applicationContext4.xml
[html] view
plain copy
- <!-- 引入外部的属性文件 -->
- <context:property-placeholder location="classpath:jdbc.properties"/>
- <!-- 配置c3p0连接池 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverClass}" />
- <property name="jdbcUrl" value="${jdbc.url}" />
- <property name="user" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- 10. </bean>
- 11.
- 12. <!-- 配置业务层类 -->
- 13. <bean id="accountService" class="com.zs.spring.demo4.AccountServiceImpl">
- 14. <property name="accountDao" ref="accountDao" />
- 15. </bean>
- 16.
- 17. <!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->
- 18. <bean id="accountDao" class="com.zs.spring.demo4.AccountDaoImpl">
- 19. <property name="dataSource" ref="dataSource" />
- 20. </bean>
- 21.
- 22. <!-- ==================================4.使用注解配置声明式事务============================================ -->
- 23.
- 24. <!-- 配置事务管理器 -->
- 25. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- 26. <property name="dataSource" ref="dataSource" />
- 27. </bean>
- 28.
- 29. <!-- 开启注解事务 -->
- 30. <tx:annotation-driven transaction-manager="transactionManager"/>
- 31.
测试:
[java] view
plain copy
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration("classpath:applicationContext4.xml")
- public class TransactionTest {
- /**
- * 一定要注入代理类:因为代理类进行增强的操作
- */
- @Resource(name = "accountService")
- private AccountService accountService;
- 10.
- 11. @Test
- 12. public void demo1() {
- 13. accountService.transfer("aaa", "bbb", 200d);
- 14. }
15. }