Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection问题可能性
报错信息:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException:
Could not get JDBC Connection; nested exception is com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Mon May 11 17:15:36 CST 2020
### The error may exist in com/juzi/cap/dao/OrderDao.java (best guess)
### The error may involve com.juzi.cap.dao.OrderDao.getByOrderId
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;
nested exception is com.alibaba.druid.pool.DataSourceClosedException: dataSource already closed at Mon May 11 17:15:36 CST 2020
原因分析
这个异常是因为主线程的数据源已经被关闭了,而子线程无法连接数据库造成的。
这个只有在“”单元测试”的时候才会出现(主线程把任务丢给线程池之后自己就直接执行完毕导致关闭了数据源),如果是生产环境主线程是不会关闭的,所以这个问题不会出现。
解决方法:
如果是单元测试可进行线程睡眠,让主线程等待线程池的线程执行完再执行
如代码:
问题所在