报错信息:
1,The last packet successfully received from the server was 59,002 milliseconds ago. 
	 The last packet sent successfully to the server was 1 milliseconds ago.
2,Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes,
	 read 0 bytes before connection was unexpectedly lost.	
3,ERROR [com.alibaba.druid.pool.DruidDataSource] - discard connection com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
数据库连接池配置:
	<bean id="emp_dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="jdbc:mysql://192.168.2.111:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&autoReconnect=true" />
		<property name="username" value="root" />
		<property name="password" value="root" />
		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxActive" value="20" />
		<property name="maxWait" value="60000" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="true" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
	</bean>

报错原因:
mysql 连接闲置一定时间后,mysql数据库就会自动断开连接,spring 的 jdcp 连接池中的connection是无效的,导致报错(使用数据库连接工具同样也会报错)
解决方法:

修改连接等待时间,默认是28800,8小时,

1,不重启mysql数据库:
SET GLOBAL interactive_timeout = 31536000;
FLUSH PRIVILEGES;

SET GLOBAL wait_timeout = 31536000;
FLUSH PRIVILEGES;
2,修改配置文件my.cnf,添加下面两个参数,重启mysql数据库:
[mysqld]
interactive_timeout = 31536000
wait_timeout = 31536000
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐