Spring Boot连接MySQL长时间不连接后报错`com.mysql.cj.core.exceptions.ConnectionIsClosedException: No operations allowed after connection closed.`的解决办法

报错:com.mysql.cj.core.exceptions.ConnectionIsClosedException: No operations allowed after connection closed.

添加 &autoReconnect=true 无济于事,此选项好像仅对 MySQL 5之前的版本有效。

原因

Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection。这就是问题的所在,在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有Client请求connection,C3P0将该失效的Connection提供给Client,将会造成上面的异常。

解决办法

http://www.cnblogs.com/hemingwang0902/archive/2012/03/15/2397620.html

临时解决办法

因为上面的办法需要修改数据库,但是我不想改数据库。所以:
修改一下连接MySQL的驱动(修改application.properties):

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

改为

spring.datasource.driverClassName=com.mysql.jdbc.Driver

猜你喜欢

转载自www.cnblogs.com/zifeiy/p/9282230.html