Java + Tomcat,Dying数据库连接? [英] Java+Tomcat, Dying database connection?

查看:164
本文介绍了Java + Tomcat,Dying数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tomcat实例设置但是我在context.xml中配置的数据库连接在不活动时段后仍然会死。

I have a tomcat instance setup but the database connection I have configured in context.xml keeps dying after periods of inactivity.

当我检查日志时,我得到了以下错误:

When I check the logs I get the following error:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
中的最后一个数据包从服务器was68051秒
前成功接收。成功发送到服务器的最后一个数据包是
前的68051秒,这比服务器配置的
'wait_timeout'值长。您应该考虑在应用程序中使用之前使
连接有效期到期和/或测试,增加
服务器配置的客户端超时值,或者使用Connector / J
连接属性'autoReconnect = true '避免这个问题。

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was68051 seconds ago. The last packet sent successfully to the server was 68051 seconds ago, which is longer than the server configured value of '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.

这是context.xml中的配置:

Here is the configuration in context.xml:

<Resource name="dataSourceName" 
        auth="Container" 
        type="javax.sql.DataSource"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000" 
        username="username" 
        password="********"
        removeAbandoned = "true"
        logAbandoned = "true"
        driverClassName="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://127.0.0.1:3306/databasename?autoReconnect=true&amp;useEncoding=true&amp;characterEncoding=UTF-8"  />

我正在使用autoReconnect = ture,就像错误说的那样,但连接一直在死。我以前从未见过这种情况。

I am using autoReconnect=ture like the error says to do, but the connection keeps dying. I have never seen this happen before.

我还验证了所有数据库连接都已正确关闭。

I have also verified that all database connections are being closed properly.

推荐答案

Tomcat文档

DBCP使用Jakarta-Commons数据库连接池。它依赖于雅加达-共享部件的数目:

DBCP uses the Jakarta-Commons Database Connection Pool. It relies on number of Jakarta-Commons components:

* Jakarta-Commons DBCP
* Jakarta-Commons Collections
* Jakarta-Commons Pool

此属性可能会帮助你。

removeAbandonedTimeout="60"

我正在使用相同的连接池,我正在设置这些属性以防止它只是没有通过tomcat配置。
但是,如果第一件事不工作尝试这些。

I'm using the same connection pooling stuff and I'm setting these properties to prevent the same thing it's just not configured through tomcat. But if the first thing doesn't work try these.

testWhileIdle=true
timeBetweenEvictionRunsMillis=300000

这篇关于Java + Tomcat,Dying数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆