1. 打断点本地测试异步处理,在异步处理子线程里操作数据库报数据库连接池已关闭错误
这里使用 SpringBoot 自带的数据库连接池(HikariDataSource)为例
<!-- 错误1 -->
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: HikariDataSource HikariDataSource (HikariPool-1) has been closed.
<!-- 错误2 -->
java.sql.SQLException: Connection has already been closed
解决方法:
本地测试时,在主线程里加个线程休眠(Thread.sleep()),以确保子线程可以执行完毕。