JDBC 连接池未在 Tomcat 中重新打开连接

发布于 2024-08-14 04:53:10 字数 479 浏览 12 评论 0原文

我已将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小镇女孩 2024-08-21 04:53:10

尝试添加验证查询属性。这应该具有在超时后自动关闭并重新打开连接的效果,如下所示:

validationQuery="SELECT 1"

Try adding a validation query attribute. This should have the effect of automatically closing and re-opening the connection after a timeout like this:

validationQuery="SELECT 1"
冷了相思 2024-08-21 04:53:10

首先,删除 autoReconnect 属性。连接池不需要它,并且可能会导致问题。

其次,确保您关闭 JDBC 代码中的所有资源(ConnectionStatementResultSet)。 code>finally 块。

我不确定这是否适用于您的情况,但初学者之间的一个常见误解是,他们似乎认为在池连接的情况下您不需要关闭这些资源。这是不真实的。池化连接是一个连接的包装器(装饰器),它有一个稍微改变的 close() 方法,大致看起来像

public void close() throws SQLException {
    if (this.connection is still active) {
        do not close this.connection, but just return it to pool for reuse;
    } else {
        actually invoke this.connection.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 and ResultSet) in your JDBC code in the finally 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 like

public void close() throws SQLException {
    if (this.connection is still active) {
        do not close this.connection, but just return it to pool for reuse;
    } else {
        actually invoke this.connection.close();
    }
}

With 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.

徒留西风 2024-08-21 04:53:10

由于这是紧急的并且对于生产来说,我建议您查看一个不错的连接池,例如 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.

终止放荡 2024-08-21 04:53:10

根据您的配置,如果空闲,则不应创建另一个连接。尝试添加

  minIdle="3"

使用此设置,DBCP 将始终保持 3 个连接。

我们在一台很少使用的服务器上看到了完全相同的行为。由于默认的连接超时为8小时,所以我们早上来的时候没有看到任何连接。这正是我们所期望的。但是,有时我们会看到连接失效,并且第一个请求会失败。要解决此问题,您需要添加以下属性,

testWhileIdle="true",
timeBetweenEvictionRunsMillis="60000"

With your configuration, it's not supposed to create another connection if it's idle. Try to add

  minIdle="3"

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,

testWhileIdle="true",
timeBetweenEvictionRunsMillis="60000"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文