Java+Tomcat,数据库连接死机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java+Tomcat, Dying database connection?
提问by Matt MacLean
I have a tomcat instance setup but the database connection I have configured in context.xml
keeps dying after periods of inactivity.
我有一个 tomcat 实例设置,但我配置的数据库连接context.xml
在一段时间不活动后一直死机。
When I check the logs I get the following error:
当我检查日志时,我收到以下错误:
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.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:从服务器成功接收的最后一个数据包是 68051 秒前。最后一个成功发送到服务器的数据包是 68051 秒前,这比服务器配置的 'wait_timeout' 值要长。您应该考虑在应用程序中使用之前使连接有效性过期和/或测试连接有效性,增加客户端超时的服务器配置值,或使用连接器/J 连接属性“autoReconnect=true”来避免此问题。
Here is the configuration in context.xml:
这是 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&useEncoding=true&characterEncoding=UTF-8" />
I am using autoReconnect=true
like the error says to do, but the connection keeps dying. I have never seen this happen before.
我autoReconnect=true
像错误所说的那样使用,但连接不断死亡。我以前从未见过这种情况。
I have also verified that all database connections are being closed properly.
我还验证了所有数据库连接都已正确关闭。
采纳答案by ScArcher2
DBCP uses the Jakarta-Commons Database Connection Pool. It relies on number of Jakarta-Commons components:
DBCP 使用 Jakarta-Commons 数据库连接池。它依赖于 Jakarta-Commons 组件的数量:
* Jakarta-Commons DBCP
* Jakarta-Commons Collections
* Jakarta-Commons Pool
This attribute may help you out.
这个属性可以帮到你。
removeAbandonedTimeout="60"
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.
我正在使用相同的连接池的东西,我正在设置这些属性来防止同样的事情,它只是没有通过 tomcat 配置。但是,如果第一件事不起作用,请尝试这些。
testWhileIdle=true
timeBetweenEvictionRunsMillis=300000
回答by abyx
I do not know whether the above answer does basically the same thing, but some of our systems use the DB connection about once a week and I've seen that we provide a -Otimeout flag or something of that sort to mysql to set the connection timeout.
我不知道上面的答案是否基本相同,但是我们的一些系统大约每周使用一次数据库连接,我已经看到我们向 mysql 提供了一个 -Otimeout 标志或类似的东西来设置连接暂停。
回答by Sindri Traustason
Just to clarify what is actually causing this. MySQL by default terminates open connections after 8 hours of inactivity. However the database connection pool will retain connections for longer than that.
只是为了澄清究竟是什么导致了这种情况。默认情况下,MySQL 在 8 小时不活动后终止打开的连接。但是,数据库连接池将保留连接的时间更长。
So by setting timeBetweenEvictionRunsMillis=300000 you are instructing the connection pool to run through connections and evict and close idle ones every 5 minutes.
因此,通过设置 timeBetweenEvictionRunsMillis=300000,您可以指示连接池每 5 分钟运行一次连接并逐出和关闭空闲连接。
回答by Ophir
The removeAbandoned option is deprecated as of DBCP 1.2 (though still presentin the 1.3 branch). Here's a non-official explanation.
从 DBCP 1.2 开始不推荐使用 removeAbandoned 选项(尽管仍然存在于 1.3 分支中)。这是一个非官方的解释。