今天爱分享给大家带来ERROR org.hibernate.util.JDBCExceptionReport Communications link failure【解决方法】,希望能够帮助到大家。
原项目用的是mysql 5.7,后调整为mysql 8.0后,数据库驱动有问题,可按需调整pom文件。
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
但调整驱动后,发现java.net.ConnectException: Connection timed out: connect,以及 ERROR (org.hibernate.util.JDBCExceptionReporter:101)- Communications link failure
具体报错如下:
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(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.__newInstance(DelegatingConstructorAccessorImpl.java:45) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45009) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45012) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) at com.mysql.cj.NativeSession.connect(NativeSession.java:144) at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ... 9 more Caused by: java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ... 12 more [2020-12-25 20:18:18.403] [] [] [] [] [--]-[] WARN (org.hibernate.util.JDBCExceptionReporter:100)- SQL Error: 0, SQLState: 08S01 [2020-12-25 20:18:18.404] [] [] [] [] [--]-[] ERROR (org.hibernate.util.JDBCExceptionReporter:101)- Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. [2020-12-25 20:18:18.405] [] [] [] [] [--]-[] WARN (org.hibernate.util.JDBCExceptionReporter:100)- SQL Error: 0, SQLState: 08S01 [2020-12-25 20:18:18.406] [] [] [] [] [--]-[] ERROR (org.hibernate.util.JDBCExceptionReporter:101)- Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
原因分析:
mysql存在一项属性“wait_timeout”,默认值为28800秒(8小时),
其意思为mysql的一个connection空闲时间超过8小时,mysql会自动断开该连接。
由于dbcp没有检验该connection是否有效,所以用其进行数据操作便会出现异常。
解决办法为:
调整/etc/my.cnf 数据库配置文件,增加以下配置:
interactive_timeout = 31536000 wait_timeout = 31536000
然后重启mysql。
service restart mysql
我用的是docker ,重启命令为:
docker restart mysql