简体   繁体   English

连接池在Tomcat 7中以静默方式过期,但autoReconnect = true无法修复它

[英]Connection pool expires silently in Tomcat 7 but autoReconnect=true doesn't fix it

I have been getting these exceptions for a number of weeks with no solution... 我已经获得这些例外数周而没有解决方案......

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 179,695,604 milliseconds ago. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功收到的最后一个数据包是179,695,604毫秒前。

The last packet sent successfully to the server was 179,695,604 milliseconds ago. 成功发送到服务器的最后一个数据包是179,695,604毫秒前。 is longer than the server configured value of 'wait_timeout'. 比服务器配置的'wait_timeout'值长。 You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem 您应该考虑在应用程序中使用之前到期和/或测试连接有效性,增加服务器配置的客户端超时值,或使用Connector / J连接属性'autoReconnect = true'来避免此问题

So I have altered my application's Context.xml to have the autoReconnect=true tag set with my databases for connection pooling in Tomcat 7. I have even set wait_timeout to infinity in the context file. 所以我更改了我的应用程序的Context.xml,以便在Tomcat 7中使用我的数据库设置autoReconnect=true标记用于连接池。我甚至在上下文文件中将wait_timeout设置为无穷大。

What am I missing? 我错过了什么? Is this a common problem? 这是个常见的问题吗? It seems to have a small amount of information around on the net, but when I follow these guides, the same thing happens the next day after a period of inactivity. 它似乎在网上有少量信息,但是当我按照这些指南时,在一段时间不活动后的第二天也会发生同样的事情。

The more I use the server, the less this happens. 我使用服务器越多,发生的情况就越少。 I think it is expiration of the pool connections but how can I stop them expiring if wait_timeout is failing? 我认为它是池连接的到期但是如果wait_timeout失败,如何阻止它们到期? Any ideas on how to diagnose the problem or config files? 有关如何诊断问题或配置文件的任何想法?

The MySQL Connector/J documentation says about autoReconnect : 关于autoReconnectMySQL Connector / J文档说:

If enabled the driver will throw an exception for a queries issued on a stale or dead connection, which belong to the current transaction, but will attempt reconnect before the next query issued on the connection in a new transaction. 如果启用,驱动程序将为在旧连接或死连接上发出的查询抛出异常,这些查询属于当前事务,但会在新事务中在连接上发出下一个查询之前尝试重新连接。

Meaning you will still get exceptions. 这意味着你仍然会得到例外。

Connectors such as JDBI get around this by adding optional test queries on idle, borrow from pool and return to pool. JDBI这样的连接器通过在空闲时添加可选的测试查询,从池中借用并返回池来解决这个问题。 Perhaps you could add something to your own JDBC connection wrapper to do the same. 也许你可以在你自己的JDBC连接包装器中添加一些东西来做同样的事情。 Alternatively, follow the documentation for autoReconnect and properly catch SQLExceptions arising from dead/stale connections. 或者,请按照autoReconnect的文档进行操作,并正确捕获死/过时连接引起的SQLExceptions。

There are further useful references on this answer using DBCP and c3p0 使用DBCP和c3p0 对此答案还有其他有用的参考

I was facing a similar problem, autoReconnect=true throws the CommunicationsException exception, but then creates a new connection to mysql. 我遇到了类似的问题,autoReconnect = true抛出了CommunicationsException异常,但随后创建了一个到mysql的新连接。 So the next request would succeed. 所以下一个请求会成功。 This behavior would continue to occur and the first request after an idle time would fail. 此行为将继续发生,并且空闲时间之后的第一个请求将失败。 To add to what Alex has already answered, I added the following params to my JDBC connection string and I don't see the error any more. 为了添加Alex已经回答的内容,我将以下参数添加到我的JDBC连接字符串中,我不再看到错误了。

 testOnBorrow="true" validationQuery="SELECT 1" validationInterval="60000"

The description of testOnBorrow sufficiently explains it. testOnBorrow的描述充分解释了它。 And the good thing is I do not have to make any changes in my code. 好消息是我不必对代码进行任何更改。

References: https://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency 参考文献: https//tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html http://www.tomcatexpert.com/blog/2010/04/01/configuring-jdbc-pool-high-concurrency

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM