博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring结合mybatis不用手动关闭sqlSession 原理
阅读量:7182 次
发布时间:2019-06-29

本文共 3068 字,大约阅读时间需要 10 分钟。

hot3.png

1,这里我用了tk mapper为例,我们首先看MapperAutoConfiguration类,它给我们注入了一个SqlSessionTemplate

@Bean    @ConditionalOnMissingBean    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {        ExecutorType executorType = this.properties.getExecutorType();        if (executorType != null) {            return new SqlSessionTemplate(sqlSessionFactory, executorType);        } else {            return new SqlSessionTemplate(sqlSessionFactory);        }    }

2,目光转移到SqlSessionTemplate,看其构造方法,发现它使用了一个代理,而实际的增删改查方法,实际是靠代理来执行的

public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,      PersistenceExceptionTranslator exceptionTranslator) {//...    this.sqlSessionFactory = sqlSessionFactory;    this.executorType = executorType;    this.exceptionTranslator = exceptionTranslator;    this.sqlSessionProxy = (SqlSession) newProxyInstance(        SqlSessionFactory.class.getClassLoader(),        new Class[] { SqlSession.class },        new SqlSessionInterceptor());  } public 
T selectOne(String statement) { return this.sqlSessionProxy.
selectOne(statement); } public
List
selectList(String statement) { return this.sqlSessionProxy.
selectList(statement); } public int update(String statement) { return this.sqlSessionProxy.update(statement); }

3,目光转移到SqlSessionInterceptor,相信看到这里,各位小伙伴都廓然开朗了吧,原来就是这个拦截器,帮我们获取提交和关闭SqlSession

private class SqlSessionInterceptor implements InvocationHandler {    @Override    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {      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) {          // release the connection to avoid a deadlock if the translator is no loaded. See issue #22          closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);          sqlSession = null;          Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);          if (translated != null) {            unwrapped = translated;          }        }        throw unwrapped;      } finally {        if (sqlSession != null) {          closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);        }      }    }  }

 

转载于:https://my.oschina.net/u/3574106/blog/3005054

你可能感兴趣的文章
lua 学习总结
查看>>
spring+Kafka+springmvc Demo
查看>>
基于Docker下的MySQL主从复制
查看>>
VUE 面试总结
查看>>
React Native组件开发指南
查看>>
keep-alive:组件级缓存
查看>>
flex 布局基本要点
查看>>
TextWatcher的使用及源码解析
查看>>
linux ssh 免密登录
查看>>
基于vue全家桶的webapp音乐播放器
查看>>
wepy笔记之原生小程序、vue、wepy之间的差异记录
查看>>
mpvue实现图书小程序
查看>>
Neo4j系统实战一
查看>>
Node.js Stream(流)
查看>>
js 函数柯里化 实现
查看>>
存储类区块链项目落地的关键性能指标——QoS!
查看>>
《Java编程思想》笔记
查看>>
长春新高三数理化暑假复习训练/高中理综补习班推荐
查看>>
JS中this的绑定规则
查看>>
Flutter入门进阶之旅(十六)Scaffold 脚手架
查看>>