项目开发-iBatis事务源码之开启新事务
来源:互联网 发布:vscode html自动补全 编辑:程序博客网 时间:2024/05/22 10:32
iBatis事务源码流程整理如下。主要包括事务的使用以及各个方法的调用过程。
iBatis的事务使用
iBatis只有对单个的增、删、改操作添加了默认事务处理过程,如果客户端没有开启事务,那么就为这些操作自动添加事务,以保证这些修改操作会被提交。因此,客户端多条语句的原子操作,需要自己调用iBatis的事务方法,将这些操作封装在事务中来实现。典型的事务使用代码如下:
* try { * <b>sqlMap.startTransaction()</b> * Employee emp2 = new Employee(); * // ...set emp2 data * Integer generatedKey = (Integer) <b>sqlMap.insert ("insertEmployee", emp2)</b>; * emp2.setFavouriteColour ("green"); * <b>sqlMap.update("updateEmployee", emp2)</b>; * <b>sqlMap.commitTransaction()</b>; * } finally { * <b>sqlMap.endTransaction()</b>; * }这是SqlMapClient类的注释部分,该类的注释本质上就是iBatis的SqlMapClient的API文档。例如:开户流程中需要保证账户和人员信息的原子性。示例如下
SqlMapClient client = SimpleExample.getInstance();try {client.startTransaction();SimpleExample.insertPerson(p);SimpleExample.insertAccount(account);client.commitTransaction();} catch (SQLException execption) {}finally{try {client.endTransaction();} catch (SQLException e) {e.printStackTrace();}}
startTransaction
1)SqlMapClientImpl提供的startTransaction()方法
public void startTransaction() throws SQLException { getLocalSqlMapSession().startTransaction(); }
protected SqlMapSessionImpl getLocalSqlMapSession() { SqlMapSessionImpl sqlMapSession = (SqlMapSessionImpl) localSqlMapSession.get(); if (sqlMapSession == null || sqlMapSession.isClosed()) { sqlMapSession = new SqlMapSessionImpl(this); localSqlMapSession.set(sqlMapSession); } return sqlMapSession; }2)SqlMapSessionImpl类的startTransaction()
public SqlMapSessionImpl(SqlMapClientImpl client) { this.delegate = client.getDelegate(); this.sessionScope = this.delegate.beginSessionScope(); this.sessionScope.setSqlMapClient(client); this.sessionScope.setSqlMapExecutor(client); this.sessionScope.setSqlMapTxMgr(client); this.closed = false; }
public void startTransaction() throws SQLException { delegate.startTransaction(sessionScope); }3)SqlMapExecutorDelegate的startTransaction()
public void startTransaction(SessionScope sessionScope) throws SQLException { try { txManager.begin(sessionScope); } catch (TransactionException e) { throw new NestedSQLException("Could not start transaction. Cause: " + e, e); } }4)TransactionManager的begin()方法
public void begin(SessionScope sessionScope, int transactionIsolation) throws SQLException, TransactionException { Transaction trans = sessionScope.getTransaction(); TransactionState state = sessionScope.getTransactionState(); if (state == TransactionState.STATE_STARTED) { throw new TransactionException("TransactionManager could not start a new transaction. " + "A transaction is already started."); } else if (state == TransactionState.STATE_USER_PROVIDED) { throw new TransactionException("TransactionManager could not start a new transaction. " + "A user provided connection is currently being used by this session. " + "The calling .setUserConnection (null) will clear the user provided transaction."); } trans = config.newTransaction(transactionIsolation); sessionScope.setCommitRequired(false); sessionScope.setTransaction(trans); sessionScope.setTransactionState(TransactionState.STATE_STARTED); }5)TransactionConfig的newTransaction()代码
public Transaction newTransaction(int transactionIsolation) throws SQLException, TransactionException { return new JdbcTransaction(dataSource, transactionIsolation); }该操作执行完成后,SqlMapSessionImpl的属性SessionScope中存储了当前会话的事务的信息:当前新建的事务对象,以及事务的状态(新建状态)和提交标识(false)。
0 0
- 项目开发-iBatis事务源码之开启新事务
- 项目开发-iBatis事务源码之SQL执行
- 项目开发-iBatis事务源码之事务提交和回滚
- 项目开发-iBatis事务结构
- ibatis源码浅析- 事务
- Ibatis 事务
- ibatis事务
- iBATIS事务
- ibatis事务
- 开启事务
- tcc分布式事务源码解析系列(一)之项目结构
- NHiberante源码分析之: 事务
- Redis源码分析之事务
- 项目开发-iBatis源码阅读
- ibatis DAO 事务探索
- ibatis的事务
- iBatis中使用事务
- Ibatis使用事务
- 什么是iBeacon
- USB HID协议中几个关键概念的理解
- 【机器学习】(6):参数方法与多元推广
- Android布局提示TextView可以代替LinearLayout警告的解决办法
- 城市级联js
- 项目开发-iBatis事务源码之开启新事务
- 现代软件学院入围全国软件专业人才设计大赛(组图)
- 黑马程序员——C语言基础篇---宏定义、数组、字符串和函数
- CacheConcurrencyStrategy 五种缓存方式 (实体类添加)
- Oracle DBMS_UTILITY.GET_HASH_VALUE
- 如何关闭U盘、移动硬盘、硬盘的自动打开功能!
- 类别中使用属性
- UVa 1602 Lattice Animals
- Elasticsearch之Nested Object