报错信息:
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&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