MyBatis如何关闭数据库连接,无感释放
目录
MyBatis如何关闭数据库连接,无感释放
MyBatis如何关闭数据库连接,无感释放
无论是原生的mybatis还是spring,我们在使用过程中对获取连接、释放连接都是无感的。那么我们在日常开发过程中,连接池是怎么和我们的mapper联系起来的呢?
实际上无论是原生的mybatis还是spring,我们在每次执行SQL语句时才会真正去获取一个连接,而原生的mybatis则需要我们去手动释放,spring则不需要。
关键类: private class SqlSessionInterceptor implements InvocationHandler{}
分析:
重点:
代理执行
代理关闭
伪代码:
try{
//执行sql
}catch(E e){
} finally{
//释放连接
if (sqlSession != null) {
closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);//如果不报错,finally也关闭sqlsession
}
}