SqlSession 的关闭是否可以这样理解
我从源代码中查看到了关闭方法 DefaultSqlSession
public void close() {
try {
this.executor.close(this.isCommitOrRollbackRequired(false));
this.closeCursors();
this.dirty = false;
} finally {
ErrorContext.instance().reset();
}
}真正关闭的是从executor这个接口的实现类关闭的,这个接口的实现类有两个BaseExecutor CachingExecutor
从源码中看,如果是直连数据库就会调用BaseExecutor中的关闭方法.
public void close(boolean forceRollback) {
try {
try {
this.rollback(forceRollback);
} finally {
if (this.transaction != null) {
this.transaction.close();
}
}
} catch (SQLException var11) {
log.warn("Unexpected exception on closing transaction. Cause: " + var11);
} finally {
this.transaction = null;
this.deferredLoads = null;
this.localCache = null;
this.localOutputParameterCache = null;
this.closed = true;
}
}this.transaction.close(); 又调到了JdbcTransaction类的close方法,确实调用了connection.close()方法
public void close() throws SQLException {
if (this.connection != null) {
this.resetAutoCommit();
if (log.isDebugEnabled()) {
log.debug("Closing JDBC Connection [" + this.connection + "]");
}
this.connection.close();
}
}而CachingExecutor是在使用连接池调用的方法,如下对事务进行了提交,但是连接是如何又放回到连接池呢?我看代码是调用了TransactionalCacheManager这里面通过HashMap存储了TransactionalCache. 提交事务以后实际上是将TransactionalCache reset了
public void close(boolean forceRollback) {
try {
if (forceRollback) {
this.tcm.rollback();
} else {
this.tcm.commit();
}
} finally {
this.delegate.close(forceRollback);
}
}public void commit() {
if (this.clearOnCommit) {
this.delegate.clear();
}
this.flushPendingEntries();
this.reset();
}11
收起
正在回答 回答被采纳积分+1
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星