这里写目录标题

  • 前言
  • 源码
  • 遇到错误 `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相关推荐

  1. java.sql.SQLException: Connection is closed

    如果没用第三方组件来配置数据源,程序会默认使用tomcat容器内建的DHCP连接池组建. <Resource auth="Container" driverClassName ...

  2. Cause: java.sql.SQLException: connection closed问题排查、解决

    connection closed 获取到的连接已经失效,导致抛出异常: message:com.framework.smart.admin.common.exception.AdminExcepti ...

  3. java.sql.SQLException: connection holder is null

    解决方案一: <!-- 是否自动回收超时连接 --> <property name="removeAbandoned" value="true" ...

  4. Druid java.sql.SQLException: connection holder is null

    问题 java.sql.SQLException: connection holder is null 项目使用了Druid,执行过程中出现了这个错误. 解决过程 错误的意思是没有获取到连接,怀疑是同 ...

  5. 线上问题:java.sql.SQLException: connection holder is null

    1 场景复现 定时更新MySQL数据到缓存中:Map存储. A表和B表均在一个定时任务中查询数据,A表先查,B表后查, B表查询时,出现java.sql.SQLException: connectio ...

  6. Cause: java.sql.SQLException: connection closed

    近期系统频繁报 java.sql.SQLException: connection closed message:com.noahgroup.framework.smart.admin.common. ...

  7. java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed

    1.本问题出现在动力节点crm项目中的p85-p88 2.出现问题时applicationContext-datasource的配置 3.报出的异常(里面有只读异常,还有就是sql中接收的id是?) ...

  8. mybatis报错:java.sql.SQLException: 无效的列类型: 1111

     java.sql.SQLException: 无效的列类型: 1111  at oracle.jdbc.driver.OracleStatement.getInternalType(Oracle ...

  9. java.sql.SQLException: connection disabled

    解决办法: 数据库地址后面加 autoReconnect=true

最新文章

  1. c语言函数参数类型检查,内联函数在编译时是否做参数类型检查?
  2. 项目中常用的git指令
  3. sqlmap代理池_sqlmap +外部代理池绕过IP拦截
  4. 让XP远程桌面支持多用户
  5. eui自带字体是什么_阿里巴巴居然出品了两款字体,免费可商用,网友:太良心了...
  6. 打地鼠c语言代码_女白领在家玩打地鼠游戏,无意间学会python编程,还有教程有源码...
  7. activiti-explorer 启动报错 Error creating bean with name 'demoDataConfiguration'
  8. 2015.4.19 为什么footer下a的索引值那么大
  9. mybatis 不同格式日期比较大小_怎样创建一个命令函数来获得不同国家和应用程序所要求的大多数日期格式...
  10. JavaScript和jQuery的DOM操作
  11. django 设置媒体url_编写第一个 django 应用
  12. 社保必须交满15年才能享受吗?
  13. ITU-R BT.1788建议书 对多媒体应用中视频质量的主观评估方法
  14. LinkedList 链表总结
  15. Python安装时import matplotlib.pyplot as plt报错
  16. MySQL 5.7安装(多实例)最佳实践
  17. mybatis动态sql,条件判断详解示例
  18. miui怎么用第三方图标包_非常棒的安卓第三方桌面lawnchair体验 “Google Pixel” 样式...
  19. 马云开酒吧了,酒吧叫“平头哥”,真猛~
  20. 真正厉害的人,都是“反内耗”体质

热门文章

  1. CentOS 7 安装MySQL 5.6遇到问题及解决方案
  2. SQL Server 跨库同步数据
  3. 修改Linux字体出现乱码
  4. jQuery 常用方法总结
  5. 多媒体调度系统如何实现对水库大坝的防洪调度
  6. CISCO2621 基本配置步骤
  7. java 调整数组顺序使奇数位于偶数前面
  8. WebBrowser控件跨域访问页面内容
  9. 关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案
  10. tensorflow与numpy的版本兼容性问题(亲测)