mybatis的getSqlSession().getConnection()关闭的链接

发布于 2021-11-22 23:55:22 字数 4135 浏览 888 评论 6

使用MyBatis-Spring后DAO层继承SqlSessionDaoSupport,然后里面就可以直接getSqlSession()从而得到mybatis的SqlSession,然后就可以增,删,改,查之类的操作。现在问题来了,SqlSession提供的功能不能满足我的要求,我要直接运行自己写的SQL。那好吧, 我就getSqlSession().getConnection();从而得到一个Connection,然后去运行自己的任意SQL就完事了,如果仅仅是这样,一切看起来都很完美。

but!我拿着Connection其运行SQL的时候,居然是“关闭的链接”,

java.sql.SQLRecoverableException: 关闭的连接
    at oracle.jdbc.driver.PhysicalConnection.createStatement(PhysicalConnection.java:3371)
    at oracle.jdbc.driver.PhysicalConnection.createStatement(PhysicalConnection.java:3346)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.ibatis.logging.jdbc.ConnectionLogger.invoke(ConnectionLogger.java:43)
    at $Proxy7.createStatement(Unknown Source)
    at com.myweb.rule.util.DDLSQLUtil.runDDLSQL(DDLSQLUtil.java:84)
    at com.myweb.rule.dao.impl.ProductDaoImpl.creat(ProductDaoImpl.java:107)
    at com.myweb.rule.dao.ProductDaoTest.testCreat(ProductDaoTest.java:136)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

这是什么情况,怎么解决呢?如果Connection是关闭的,那么用得到它的SqlSession去执行mapper.xml配置文件中的sql时却能够正常执行,这是什么原因呢?

同时说明,我之所以要getSqlSession().getConnection()获得Connection是考虑到spring和mybatis结合时的事务管理问题,spring事务管理时依靠的我猜测可能就是这个Connection;而getSqlSession().getConfiguration().getEnvironment().getDataSource().getConnection();这样虽然可以得到一个可以正常执行SQL不报“关闭的链接”的错的Connection,但貌似这种方式已经追踪到了DataSource上去获得,此时的Connection可能已经跳出了spring的事务管理,而这不是我希望的。

 

如上,我该如何解决“关闭的链接”这个问题呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

狼亦尘 2021-11-25 20:02:12

我也遇到这种问题

悲喜皆因你 2021-11-25 19:57:36

遇到同样的问题,谢谢lz

路还长,别太狂 2021-11-25 19:55:12

呃呃呃,晕死

秉烛思 2021-11-25 19:29:30

标签怎么加的啊?

风柔一江水 2021-11-25 07:43:04

为什么都无人问津呢。。。

陌若浮生 2021-11-24 04:03:18

本人已经找到方法:

SqlSessionTemplate st = (SqlSessionTemplate) getSqlSession();

Connection connection = SqlSessionUtils.getSqlSession(
                st.getSqlSessionFactory(), st.getExecutorType(),
                st.getPersistenceExceptionTranslator()).getConnection();

这样获得的,貌似是在事务中的,而且使用时不会报那个错。
            

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文