MySQL提供的JDBC驱动程序Connector/J在8小时之后会自动超时断开,这样会影响到连接池或是其它直接访问JDBC的程序(如JSP、Servlet等)。
有一种解决方法是在连接URL中设置autoReconnect参数,或是在连接池中处理connection的state。
第二种解决方案主要是依赖于SQLException中可以获得的SQLState,是在Java代码中捕获异常并在try-catch中尝试re-connect。MySQL实现中,"08S01"表示的是在处理query的时候遇到了网络连接问题,而40001表示的是遇到了死锁。
有一种解决方法是在连接URL中设置autoReconnect参数,或是在连接池中处理connection的state。
第二种解决方案主要是依赖于SQLException中可以获得的SQLState,是在Java代码中捕获异常并在try-catch中尝试re-connect。MySQL实现中,"08S01"表示的是在处理query的时候遇到了网络连接问题,而40001表示的是遇到了死锁。
try {
// ...
} catch (SQLException sqlEx) {
//
// The two SQL states that are 'retry-able' are 08S01
// for a communications error, and 40001 for deadlock.
//
// Only retry if the error was due to a stale connection,
// communications problem or deadlock
//
String sqlState = sqlEx.getSQLState();
if ("08S01".equals(sqlState) || "40001".equals(sqlState)) {
retryCount--;
} else {
retryCount = 0;
}
}
参考资料:
http://forums.mysql.com/read.php?39,112048,112146#msg-112146
http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-troubleshooting.html#qandaitem-24-4-5-3-4
http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
评论