随笔 - 256  文章 - 2  评论 - 18  阅读 - 123万

Caused by: com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed

报错场景:spring boot+mybatis,线程池执行批量任务。springboot正常启动后,定时任务中数据库查询报错。报错信息如下:

复制代码
 1 Caused by: org.apache.ibatis.exceptions.PersistenceException: 
 2 ### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; 
 3 nested exception is com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Wed Jun 30 17:31:57 GMT+08:00 2021
 4 ### The error may exist in URL [jar:file:/home/xxx/xxx/xxx-1.0.jar!/BOOT-INF/lib/common-1.0.jar!/mapper/basedata_mapper/xxxMapper.xml]
 5 ### The error may involve com.xxx.common.basedata.dao.xxxTaskDao.getListByCode
 6 ### The error occurred while executing a query
 7 ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Wed Jun 30 17:31:57 GMT+08:00 2021
 8     at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
 9     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
10     at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
11     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
12     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
13     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
14     at java.lang.reflect.Method.invoke(Method.java:498)
15     at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
16     ... 15 common frames omitted
报错信息
复制代码

 

解决:

多线程批量处理的时候只需要在service方法上加上@transactional(rollbackFor = Exception.class)就行了,mybatis就不会每次执行完sql后closing sql session了
Sign up for free

 

参考:

https://github.com/alibaba/druid/issues/1625

posted on   腾逸  阅读(11758)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 我与微信审核的“相爱相杀”看个人小程序副业
· DeepSeek “源神”启动!「GitHub 热点速览」
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示