Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
 
The last packet successfully received from the server was 60,061 milliseconds ago.  The last packet sent successfully to the server was 60,060 milliseconds ago.
        at sun.reflect.GeneratedConstructorAccessor305.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

1.检查jdbc连接串

检查这个是因为,如果sql查询时间过长,但是没有配置timeout,会造成sql还在运行,但是连接认为查询超时了就报错了。(其实如果不配这个,会使用更底层的配置,连接池、数据库配置等)

这个报错首先是检查jdbc连接串有无设置

&connectTimeout=60000&socketTimeout=60000

connectTimeout

指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。

socketTimeout

指客户端从服务器读取数据的timeout,超出后会抛出SocketTimeOutException

示例

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8&connectTimeout=60000&socketTimeout=60000

2.检查SQL运行时间

把SQL放到数据库去单独运行,例如使用native客户端运行,测试一下单独运行的时间。如果过长就要优化数据库的查询,例如优化SQL语句、增加相关表的索引等。再不行就把逻辑放到代码里,数据库只做简单的查询。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐