TransactionTemplate源码分析

事务(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 -> rollback
				rollbackOnException(status, ex);
				throw ex;
			}
			catch (Error err) {
				// Transactional code threw error -> rollback
				rollbackOnException(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()方法,将数据持久化到数据库中。

其它就是一些清理工作


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值