TransactionTemplate源码分析

来源:互联网 发布:高级软件测试简历模板 编辑:程序博客网 时间:2024/06/02 10:02

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

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


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

count = (Integer) transactionTemplate.execute(new TransactionCallback() {                        public Object doInTransaction(TransactionStatus status) {                            // 将原来的默认地址,取消默认设置                            receiveAddressDAO.updateDefaultAddressToUnDefault(memberId);                            // 更新地址                            int row = receiveAddressDAO.updateReceiveAddress(propertiesMap);                            if (row > 0) {                                return row;                            } else {                                // 抛出异常,回滚事务                                throw new RuntimeException(                                                           "[ReceiveAddressService] updateReceiveAddress failed. propertiesMap is "                                                                   + propertiesMap);                            }                        }                    });

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


public Object execute(TransactionCallback action) throws TransactionException {if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) {return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action);}else {TransactionStatus status = this.transactionManager.getTransaction(this);Object result = null;try {result = action.doInTransaction(status);}catch (RuntimeException ex) {// Transactional code threw application exception -> rollbackrollbackOnException(status, ex);throw ex;}catch (Error err) {// Transactional code threw error -> rollbackrollbackOnException(status, err);throw err;}this.transactionManager.commit(status);return result;}}

先了解execute方法的大致结构

其流程分为三部分:

1. 获取一个事务状态

2. 执行sql

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

4. 返回结果



1. 获取一个事务状态


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

将连接的autoCommit属性设置为false

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


2. 执行sql

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


3. 事务提交


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

其它就是一些清理工作


原创粉丝点击