与 MySQL 数据库的 JDBC CommunicationsException

发布于 2024-08-31 17:17:03 字数 2556 浏览 4 评论 0原文

我的 MySQL 连接池遇到了一些问题。 情况是这样的:

通过 Quartz 调度不同的作业。所有作业都连接到不同的数据库,这些数据库全天工作正常,而夜间计划的作业则因 CommunicationsException 而失败...

Quartz-Jobs:

Job1 runs 0 0 6,10,14,18 * * ?
Job2 runs 0 30 10,18 * * ?
Job3 runs 0 0 5 * * ?

如您所见,最后一个作业在 18 点运行,运行时间约为 1 小时。 凌晨 5 点的第一份工作是失败的。我已经在我的资源配置中尝试了各种参数组合,这就是我现在正在运行的参数组合:

<!-- Database 1 (MySQL) -->
<Resource
 auth="Container"
 driverClassName="com.mysql.jdbc.Driver"
 maxActive="100"
 maxIdle="30"
 maxWait="10000"
 removeAbandoned="true"
 removeAbandonedTimeout="60"
 logAbandoned="true"
 type="javax.sql.DataSource"
 name="jdbc/appDbProd"
 username="****"
 password="****"
 url="jdbc:mysql://127.0.0.1:3306/appDbProd?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"
 testWhileIdle="true"
 testOnBorrow="true"
 testOnReturn="true"
 validationQuery="SELECT 1"
 timeBetweenEvictionRunsMillis="1800000"
/>

<!-- Database 2 (MySQL) -->
<Resource
 auth="Container"
 driverClassName="com.mysql.jdbc.Driver"
 maxActive="100"
 maxIdle="30"
 maxWait="10000"
 removeAbandoned="true"
 removeAbandonedTimeout="60"
 logAbandoned="true"
 type="javax.sql.DataSource"
 name="jdbc/prodDbCopy"
 username="****"
 password="****"
 url="jdbc:mysql://127.0.0.1:3306/prodDbCopy?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"
 testWhileIdle="true"
 testOnBorrow="true"
 testOnReturn="true"
 validationQuery="SELECT 1"
 timeBetweenEvictionRunsMillis="1800000"
/>
<!-- Database 3 (MSSQL)-->
<Resource
 auth="Container"
 driverClassName="net.sourceforge.jtds.jdbc.Driver"
 maxActive="30"
 maxIdle="30"
 maxWait="100"
 removeAbandoned="true"
 removeAbandonedTimeout="60"
 logAbandoned="true"
 name="jdbc/catalogDb"
 username="****"
 password="****"
 type="javax.sql.DataSource"
 url="jdbc:jtds:sqlserver://127.0.0.1:1433;databaseName=catalog;useNdTLMv2=false"
 testWhileIdle="true"
 testOnBorrow="true"
 testOnReturn="true"
 validationQuery="SELECT 1"
 timeBetweenEvictionRunsMillis="1800000"
/>

由于显而易见的原因,我更改了 IP、用户名和密码,但可以假设它们是正确的,看到应用程序在整个过程中成功运行天。

最烦人的是: 首先运行的第一个作业成功查询 Database2,但由于某种原因无法查询 Database1 (CommunicationsException):

原因: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 最后一个数据包成功接收 来自服务器的是 39,376,539 毫秒前。最后一个数据包 已成功发送至服务器 39,376,539 毫秒前。更长 比服务器配置的值 '等待超时'。你应该考虑 即将到期和/或正在测试 在您使用之前的连接有效性 应用程序,增加服务器 客户端超时的配置值, 或使用连接器/J 连接 避免使用属性“autoReconnect=true” 这个问题。

有什么想法吗?谢谢!

I'm having a little trouble with my MySQL- Connection- Pooling.
This is the case:

Different jobs are scheduled via Quartz. All jobs connect to different databases which works fine the whole day while the nightly scheduled jobs fail with a CommunicationsException...

Quartz-Jobs:

Job1 runs 0 0 6,10,14,18 * * ?
Job2 runs 0 30 10,18 * * ?
Job3 runs 0 0 5 * * ?

As you can see the last job runs at 18 taking about 1 hour to run.
The first job at 5am is the one that fails. I already tried all kinds of parameter-combinations in my resource config this is the one I am running right now:

<!-- Database 1 (MySQL) -->
<Resource
 auth="Container"
 driverClassName="com.mysql.jdbc.Driver"
 maxActive="100"
 maxIdle="30"
 maxWait="10000"
 removeAbandoned="true"
 removeAbandonedTimeout="60"
 logAbandoned="true"
 type="javax.sql.DataSource"
 name="jdbc/appDbProd"
 username="****"
 password="****"
 url="jdbc:mysql://127.0.0.1:3306/appDbProd?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"
 testWhileIdle="true"
 testOnBorrow="true"
 testOnReturn="true"
 validationQuery="SELECT 1"
 timeBetweenEvictionRunsMillis="1800000"
/>

<!-- Database 2 (MySQL) -->
<Resource
 auth="Container"
 driverClassName="com.mysql.jdbc.Driver"
 maxActive="100"
 maxIdle="30"
 maxWait="10000"
 removeAbandoned="true"
 removeAbandonedTimeout="60"
 logAbandoned="true"
 type="javax.sql.DataSource"
 name="jdbc/prodDbCopy"
 username="****"
 password="****"
 url="jdbc:mysql://127.0.0.1:3306/prodDbCopy?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"
 testWhileIdle="true"
 testOnBorrow="true"
 testOnReturn="true"
 validationQuery="SELECT 1"
 timeBetweenEvictionRunsMillis="1800000"
/>
<!-- Database 3 (MSSQL)-->
<Resource
 auth="Container"
 driverClassName="net.sourceforge.jtds.jdbc.Driver"
 maxActive="30"
 maxIdle="30"
 maxWait="100"
 removeAbandoned="true"
 removeAbandonedTimeout="60"
 logAbandoned="true"
 name="jdbc/catalogDb"
 username="****"
 password="****"
 type="javax.sql.DataSource"
 url="jdbc:jtds:sqlserver://127.0.0.1:1433;databaseName=catalog;useNdTLMv2=false"
 testWhileIdle="true"
 testOnBorrow="true"
 testOnReturn="true"
 validationQuery="SELECT 1"
 timeBetweenEvictionRunsMillis="1800000"
/>

For obvious reasons I changed IPs, Usernames and Passwords but they can be assumed to be correct, seeing that the application runs successfully the whole day.

The most annoying thing is:
The first job that runs first queries Database2 successfully but fails to query Database1 for some reason (CommunicationsException):

Caused by:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
The last packet successfully received
from the server was 39,376,539
milliseconds ago. The last packet
sent successfully to the server was
39,376,539 milliseconds ago. 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.

Any ideas? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

铁憨憨 2024-09-07 17:17:03

正如它所说,在连接池上启用连接验证。

As it says, enable connection validation on the connection pool.

以往的大感动 2024-09-07 17:17:03

当连接闲置一段时间时,我会收到此错误。您的作业是否会在某个时刻停止访问数据库,然后运行一些查询?

当链接失败时也会发生这种情况,您可能需要与您的网络团队检查连接是否没有断开。

I get this error when a connection is left idle for a while. Do your jobs stop hitting the DB at some point, then run some queries?

It also happens when there's a link failure, you might want to check with your network team if the connection doesn't drop.

故事与诗 2024-09-07 17:17:03

我在使用 Oracle 和 JDBC 时遇到了类似的问题。最终我消耗了数据库可用的所有连接,并且我没有放弃其中任何一个,我的代码只会停止或超时。看一下您如何在代码中处理连接。

I have run into a similar issue using Oracle, and JDBC. It ended up I was consuming all of the connections that were available to the database, and I wasn't relinquishing any of them and my code would just halt or timeout. Take a look at how you are handling your connections in your code.

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