mysql 连接超时异常

mysql 连接超时错误

自己的小网站在测试机器上长时间不访问后(默认8小时过期),再次访问发现有如下错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received fro

m the server was 63,020,509 milliseconds ago. ?The last packet sent successfully to the server was 63,020,509

?milliseconds ago. is longer than the server configured value of ‘wait_timeout’. You should consider either e

xpiring and/or testing connection validity before use in your application, increasing the server configured v

alues for client timeouts, or using the Connector/J connection property ‘autoReconnect=true’ to avoid this pr

oblem.

? ? ? ? at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

? ? ? ? at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

? ? ? ? at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:4

5)

? ? ? ? at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

? ? ? ? at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)

? ? ? ? at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)

? ? ? ? at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3851)

? ? ? ? at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2471)

? ? ? ? at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)

? ? ? ? at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2683)

? ? ? ? at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2144)

? ? ? ? at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)

? ? ? ? at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)

? ? ? ? at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)

?

Fix:?If only the connection pool could check if the the connection it is about to return is live or not, the porblem is fixed. This can be done in apache-common-dbcp (I know this one coz I used it, please look into documentation of the connection-pool you are using). Here’s how you do it: You add the following properties to dbcp configuration.

  • validationQuery=”SELECT 1″
  • testOnBorrow=”true”

And that does the trick.

免责声明: 本文仅代表作者个人观点,与无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。

相关资料

mysql 连接超时异常

相关文章:

你感兴趣的文章:

标签云: