JDBC 连接池未在 Tomcat 中重新打开连接
我已将 Tomcat 设置为使用连接池,但在连接上的 MySQL 超时后,之前在池中打开的连接不会打开。我的 context.xml 文件如下所示:
<Resource name="jdbc/hpsgDB" auth="Container" type="javax.sql.DataSource"
maxActive="5" maxIdle="3" maxWait="10000"
username="uname" password="password" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/hpsgdb?autoReconnect=true"/>
如您所见,我已将 autoReconnect 包含为 true,但事实并非如此。我在 8 小时后检查了数据库上的进程,这就是超时设置的值。
I have set up Tomcat to use a connection pool yet after the MySQL timeout on connections the connections previously open in the pool are not opened. Here is what my context.xml file looks like:
<Resource name="jdbc/hpsgDB" auth="Container" type="javax.sql.DataSource"
maxActive="5" maxIdle="3" maxWait="10000"
username="uname" password="password" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/hpsgdb?autoReconnect=true"/>
As you can see I have included autoReconnect as true yet it doesn't. I have checked the process on the database after 8 hours which is what the time out is set to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试添加验证查询属性。这应该具有在超时后自动关闭并重新打开连接的效果,如下所示:
Try adding a validation query attribute. This should have the effect of automatically closing and re-opening the connection after a timeout like this:
首先,删除
autoReconnect
属性。连接池不需要它,并且可能会导致问题。其次,确保您关闭 JDBC 代码中的所有资源(
Connection
、Statement
和ResultSet
)。 code>finally 块。我不确定这是否适用于您的情况,但初学者之间的一个常见误解是,他们似乎认为在池连接的情况下您不需要关闭这些资源。这是不真实的。池化连接是一个连接的包装器(装饰器),它有一个稍微改变的
close()
方法,大致看起来像换句话说,关闭它们释放池化连接以便可以将其放回池中以供将来重复使用。如果您获取连接而不关闭它们,那么池迟早会耗尽连接。
First, get rid of the
autoReconnect
property. You don't need this with a connection pool and may cause problems.Second, ensure that you close all resources (
Connection
,Statement
andResultSet
) in your JDBC code in thefinally
block.I am not sure if this applies in your case, but a common misconception among starters is that they seem to think that you don't need to close those resources in case of a pooled connections. This is untrue. A pooled connection is a wrapper (decorator) around a connection which has a slightly changed
close()
method which roughly look likeWith other words, closing them frees up the pooled connection so that it can be put back in the pool for future reuse. If you acquire connections without closing them, then the pool will run out of connections sooner or later.
由于这是紧急的并且对于生产来说,我建议您查看一个不错的连接池,例如 c3p0。它更加健壮和可靠,并且可以更好地处理超时。
Since this is urgent and for production I suggest you have look at a decent connection pool such as c3p0. It's more robust and reliable and can handle timeouts better.
根据您的配置,如果空闲,则不应创建另一个连接。尝试添加
使用此设置,DBCP 将始终保持 3 个连接。
我们在一台很少使用的服务器上看到了完全相同的行为。由于默认的连接超时为8小时,所以我们早上来的时候没有看到任何连接。这正是我们所期望的。但是,有时我们会看到连接失效,并且第一个请求会失败。要解决此问题,您需要添加以下属性,
With your configuration, it's not supposed to create another connection if it's idle. Try to add
With this setting, DBCP will maintain 3 connections all time.
We see exactly the same behavior with one of lightly used servers. Due to the default connection timeout of 8 hours, we see no connections when we come in the morning. That's what we expected. However, sometimes we see stale connection and the first request will fail. To get around this issue, you need add following attributes,