日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

【Mybatis】sqlSessionTemplate.getConnection() 遇到 java.sql.SQLException: Connection is closed

發布時間:2024/9/19 数据库 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Mybatis】sqlSessionTemplate.getConnection() 遇到 java.sql.SQLException: Connection is closed 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這里寫目錄標題

  • 前言
  • 源碼
  • 遇到錯誤 `java.sql.SQLException: Connection is closed`
  • 錯誤原因
  • 解決辦法
  • 修改后的源碼

前言

  • springboot 2.1.1.RELEASE

源碼

@Repository public class TestDao {@Autowiredprivate SqlSessionTemplate sqlSessionTemplate;public void printSysConfig() {String sql = "select * from sys_config";try (Connection conn = this.sqlSessionTemplate.getConnection();PreparedStatement ps = conn.prepareStatement(sql);) {ResultSet rs = ps.executeQuery();int cols = rs.getMetaData().getColumnCount();while (rs.next()) {for (int i=1;i<=cols;i++) {System.out.print(rs.getString(i));System.out.print(",");}System.out.println();}} catch (Exception e) {e.printStackTrace();}} }

遇到錯誤 java.sql.SQLException: Connection is closed

java.sql.SQLException: Connection is closedat com.zaxxer.hikari.pool.ProxyConnection$ClosedConnection.lambda$getClosedConnection$0(ProxyConnection.java:515)at com.sun.proxy.$Proxy50.prepareStatement(Unknown Source)at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:337)at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)at com.ymyun.samples.TestDao.printSysConfig(TestDao.java:40)at com.ymyun.samples.TestDao$$FastClassBySpringCGLIB$$20d8ee9.invoke(<generated>)at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749)at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:691)at com.ymyun.samples.TestDao$$EnhancerBySpringCGLIB$$1cf3f5d2.printSysConfig(<generated>)at com.ymyun.samples.YmyunSamplesApplication.lambda$0(YmyunSamplesApplication.java:45)at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784)at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768)at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)at com._3jcf.ymyun.samples.YmyunSamplesApplication.main(YmyunSamplesApplication.java:38)

錯誤原因

參考
https://www.cnblogs.com/Zhongzz/p/9407639.html

解決辦法

使用下面的方法獲取Connection:

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

修改后的源碼

@Repository public class TestDao {@Autowiredprivate SqlSessionTemplate sqlSessionTemplate;public void printSysConfig() {String sql = "select * from sys_config";try (Connection conn = this.getConnection();PreparedStatement ps = conn.prepareStatement(sql);) {ResultSet rs = ps.executeQuery();int cols = rs.getMetaData().getColumnCount();while (rs.next()) {for (int i=1;i<=cols;i++) {System.out.print(rs.getString(i));System.out.print(",");}System.out.println();}} catch (Exception e) {e.printStackTrace();}}private Connection getConnection() {SqlSessionTemplate st = this.sqlSessionTemplate;Connection connection = SqlSessionUtils.getSqlSession(st.getSqlSessionFactory(), st.getExecutorType(),st.getPersistenceExceptionTranslator()).getConnection();return connection;} }

總結

以上是生活随笔為你收集整理的【Mybatis】sqlSessionTemplate.getConnection() 遇到 java.sql.SQLException: Connection is closed的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。