java中MySQL错误com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
做项目企业微信留痕拉起数据存储Mysql时报错,
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2186)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2219)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2014)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.tencent.wework.tool.DBUtil.getConn(DBUtil.java:76)
at com.tencent.wework.tool.DBUtil.updateData(DBUtil.java:215)
百度查了很多资料,终于找到了解决办法
在jdbc连接url的配置中,你可以附上“autoReconnect=true” 不过这个对mysql5以前的版本起作用。增加“validation query”似乎也无济于事。
需要重新启动mysql5。
linux系统配置文件:/etc/my.cnf
测试显示问题解决了。
也可以直接设置
mysql修改wait_timeout
mysql mysql> show global variables like 'wait_timeout';
其默认值为8小时
mysql的一个connection空闲时间超过8小时,mysql会自动断开该连接。
1.修改配置
vi /etc/my.cnf
[mysqld] wait_timeout=10
# /etc/init.d/mysql restart
2.直接用sql命令行修改 mysql> set global wait_timeout=604800;
除了wait_timeout,还有一个'interactive_timeout'
同样可以执行SHOW GLOBAL VARIABLES LIKE 'interactive_timeout'来查询
执行set global interactive_timeout=604800;来设置