TransactionTemplate源码分析

来源:互联网 发布:深圳市软件产业基地5栋 编辑:程序博客网 时间:2024/05/17 21:48

出处:http://blog.csdn.net/itomge/article/details/9263181

事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。事务通常由高级数据库操纵语言或编程语言(如SQL或Java)书写的用户程序的执行所引起,并用形如begin transaction和end transaction语句。事务由事务开始(begin transaction)和事务结束(end transaction)之间执行的全体操作组成。

事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。


例如:spring的事务控制代码片段

[html] view plaincopyprint?
  1. count = (Integer) transactionTemplate.execute(new TransactionCallback() {  
  2.   
  3.                         public Object doInTransaction(TransactionStatus status) {  
  4.                             // 将原来的默认地址,取消默认设置  
  5.                             receiveAddressDAO.updateDefaultAddressToUnDefault(memberId);  
  6.                             // 更新地址  
  7.                             int row = receiveAddressDAO.updateReceiveAddress(propertiesMap);  
  8.                             if (row > 0) {  
  9.                                 return row;  
  10.                             } else {  
  11.                                 // 抛出异常,回滚事务  
  12.                                 throw new RuntimeException(  
  13.                                                            "[ReceiveAddressService] updateReceiveAddress failed. propertiesMap is "  
  14.                                                                    + propertiesMap);  
  15.                             }  
  16.                         }  
  17.                     });  

代码编写起来很简单,但是内部实现的原理是什么?


[html] view plaincopyprint?
  1. public Object execute(TransactionCallback action) throws TransactionException {  
  2.         if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) {  
  3.             return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action);  
  4.         }  
  5.         else {  
  6.             TransactionStatus status = this.transactionManager.getTransaction(this);  
  7.             Object result = null;  
  8.             try {  
  9.                 result = action.doInTransaction(status);  
  10.             }  
  11.             catch (RuntimeException ex) {  
  12.                 // Transactional code threw application exception -> rollback  
  13.                 rollbackOnException(status, ex);  
  14.                 throw ex;  
  15.             }  
  16.             catch (Error err) {  
  17.                 // Transactional code threw error -> rollback  
  18.                 rollbackOnException(status, err);  
  19.                 throw err;  
  20.             }  
  21.             this.transactionManager.commit(status);  
  22.             return result;  
  23.         }  
  24.     }  

先了解execute方法的大致结构

其流程分为三部分:

1. 获取一个事务状态

2. 执行sql

3. 如果抛异常,rollbackOnException回滚事务;否则提交事务

4. 返回结果



1. 获取一个事务状态


从数据源获取一个新的连接

将连接的autoCommit属性设置为false

TransactionSynchronizationManager将(dataSource,连接)名值对作为线程变量保存起来。
Transaction对象也保存了连接的句柄


2. 执行sql

[html] view plaincopyprint?
  1. // 将原来的默认地址,取消默认设置  
  2.                             receiveAddressDAO.updateDefaultAddressToUnDefault(memberId);  
  3.                             // 更新地址  
  4.                             int row = receiveAddressDAO.updateReceiveAddress(propertiesMap);  
  5.                             if (row > 0) {  
  6.                                 return row;  
  7.                             } else {  
  8.                                 // 抛出异常,回滚事务  
  9.                                 throw new RuntimeException(  
  10.                                                            "[ReceiveAddressService] updateReceiveAddress failed. propertiesMap is "  
  11.                                                                    + propertiesMap);  
  12.                             }  
这块内容比较简单,就是dao的DML操作


3. 事务提交


最终还是调用com.mysql.jdbc.Connection.commit()方法,将数据持久化到数据库中。

其它就是一些清理工作


0 0