MyBatis 源码解析SqlSession.getConnection(Connection异常:关闭或者不在Mybatis事务)
2024-08-25 14:58:20
从SqlSession的实现类SqlSessionTemplate源码中,看出相关方法被其内部类SqlSessionInterceptor(实现了InvocationHandler)代理
当调用SqlSessionTemplate.getConnection()时,有SqlSessionInterceptor代理执行,从SqlSessionInterceptor源码中看到其invoke方法finally代码块那内容如:
//关闭SqlSession
导致获取到的Connection中delegate(委托)的holder(存放事务对象)为空
解决方案:
抽离代理,将代理方法SqlSessionInterceptor.invoke中的代码拿出直接使用
SqlSessionTemplate st = (SqlSessionTemplate) getSqlSession(); Connection connection = SqlSessionUtils.getSqlSession(
st.getSqlSessionFactory(), st.getExecutorType(),
st.getPersistenceExceptionTranslator()).getConnection();
这样获取到的Connection可以正常使用
最新文章
- Centos7的firewalld配置
- 【管理心得之四十】中文“其他”、英文“other”、日文“その他”..........................................
- ContentProvider详解
- 运行eclipse提示 The requested resource () is not available.
- 直接运行PowerShell脚本
- WCF架构日记-1
- flex学习网站地址
- cocos2d-x 2.2.3 之菜单分析(1)
- .Net_把文件数据添加到数据库中(面试题)
- sql/plus 常用操作
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
- 用 Visual Studio Code 调试 Node.js
- Windows入门基础:2.vs2013中Icon显示
- 左耳听风-ARTS-第3周(2019/4/7-2019/4/13)
- react-native 报错 RawText ";"; must be wrapped in an explicit <;Text>; component
- jmeter获取cookies信息(配置)
- “此flash与您的地区不兼容”
- python 常见报错汇总
- 文件缓冲区在fork后复制
- 1085. Perfect Sequence