读mybatis源码之十四:mybatis事务处理
来源:互联网 发布:daxulu域名更换 编辑:程序博客网 时间:2024/05/02 04:25
1、自身事务处理
public void doxxxxx(){ TransactionFactory transactionFactory = new JdbcTransactionFactory(); userMapper userDao=getSession().getMapper(UserMapper.class); Transaction newTransaction=transactionFactory.newTransaction(getSession().getConnection()); try { userDao.insert(xxx); userDao.update(xxx); } catch (Exception e) { newTransaction.rollback(); e.printStackTrace(); } finally { newTransaction.close(); }}可以看出主要是事务连接。
2、与spring集成后,事务处理
BlogMapper mapper = (BlogMapper)app.getBean("blogMapper"); Blog sblog = new Blog(); sblog.setId(2); sblog.setName("测试加密555"); sblog.setAddr("测试加密--------------123444"); mapper.insertBlogByObj(sblog);
这种数据能够提交上去,为什么呢? 看看与spring集成后它的处理方式
首先看如何得到mapper的,通过MapperFactoryBean的getObject() 方法得到
public T getObject() throws Exception { return getSqlSession().getMapper(this.mapperInterface); }
那getSqlSession()又是如何得到的呢?跟踪是通过SqlSessionTemplate得到,SqlSessionTemplate里面是什么呢?
发现是一个sqlSessionProxy代理:
public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) { notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required"); notNull(executorType, "Property 'executorType' is required"); this.sqlSessionFactory = sqlSessionFactory; this.executorType = executorType; this.exceptionTranslator = exceptionTranslator; this.sqlSessionProxy = (SqlSession) newProxyInstance( SqlSessionFactory.class.getClassLoader(), new Class[] { SqlSession.class }, new SqlSessionInterceptor()); }具体看SqlSessionInterceptor,就可以看出为什么spring集成后,不显示提交,也能自动提交了:
private class SqlSessionInterceptor implements InvocationHandler { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { final SqlSession sqlSession = getSqlSession( SqlSessionTemplate.this.sqlSessionFactory, SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator); try { Object result = method.invoke(sqlSession, args); if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) { // force commit even on non-dirty sessions because some databases require // a commit/rollback before calling close() sqlSession.commit(true); } return result; } catch (Throwable t) { Throwable unwrapped = unwrapThrowable(t); if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) { Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped); if (translated != null) { unwrapped = translated; } } throw unwrapped; } finally { closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory); } } }
if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {
// force commit even on non-dirty sessions because some databases require
// a commit/rollback before calling close()
sqlSession.commit(true);
}
这部分重点,如果配置了事务,就由配置事务处理,没有配置就直接提交,所以这里相当于他帮你提交了。
closeSqlSession里面也是一样,如果有事务,是session释放,如果没有就是session关闭,跟踪下去就是事务关闭,mybatis spring结合后里面SpringManagedTransaction的close,用的DataSourceUtils的releaseConnection
public void close() throws SQLException {
releaseConnection(this.connection, this.dataSource);
}
0 0
- 读mybatis源码之十四:mybatis事务处理
- myBatis事务处理
- myBatis源码之XMLConfigBuilder
- myBatis源码之Configuration
- mybatis源码之MapperMethod
- mybatis源码之StatementHandler
- mybatis源码之BaseStatementHandler
- Mybatis源码之CallableStatementHandler
- mybatis源码之PreparedStatementHandler
- Mybatis源码之RoutingStatementHandler
- mybatis源码之SimpleStatementHandler
- Mybatis源码之SimpleExecutor
- myBatis源码之BatchExecutor
- MyBatis框架中的事务处理
- 读mybatis源码之十一:参数处理器
- 读mybatis源码之十三:拦截器
- myBatis源码学习之SqlSessionFactoryBuilder
- myBatis源码学习之SqlSessionFactory
- 标记-清除算法
- 训练赛 Grouping(强连通分量缩点 + DAG求最长路)
- IOS中获取各个文件的目录路径的方法和NSFileManager类
- 欧拉函数
- jdbc连接SQL Server——非常简单,只需3步
- 读mybatis源码之十四:mybatis事务处理
- dojo小例子(17)iframe上传文件到rest后台,以及乱码问题解决
- struts关于多个时间需要转换
- 程序员技术练级攻略
- C语言几点说明
- RedHat Linux下安装JDK1.7报错Permission denied
- 决策树算法原理及JAVA实现(ID3)
- OnlineJudge框架编写日志
- 快速排序1 升序