【问题标题】:Connection drop by DB数据库连接断开
【发布时间】:2019-04-15 04:29:01
【问题描述】:

我有一个大约 20 TPS 的系统。该系统建立数据库连接以获取一些数据。我正在使用带有 c3p0 的 Hibernate 来管理我的连接,并使用 Guice 来确保使用 @transactional 注释的事务性。 由于某些数据库连接问题,将近 0.3% 的请求失败。 这就是堆栈跟踪的样子。

javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: could not prepare statement
.
.
.
Caused by: org.hibernate.exception.JDBCConnectionException: could not prepare statement
.
.
.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed

.
.
.
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
.
.
.
Caused by: java.net.SocketException: Connection reset

我的休眠 c3p0 设置是:

  hibernate.connection.url = "jdbc:mysql://dbHost.com:3306/DB";
  hibernate.dialect = "org.hibernate.dialect.MySQLDialect";
  hibernate.cache.provider_class = "org.hibernate.cache.NoCacheProvider";
  hibernate.hbm2ddl.auto = "update";
  hibernate.show_sql = "false";
  hibernate.connection.provider_class = "org.hibernate.c3p0.internal.C3P0ConnectionProvider";
  hibernate.c3p0.min_size = "100";
  hibernate.c3p0.max_size = "100";
  hibernate.c3p0.idle_test_period = "100";
  hibernate.c3p0.max_statements = "0";
  hibernate.c3p0.timeout = "86400";
  hibernate.c3p0.preferredTestQuery = "select 1";

我的数据库系统变量:

connect_timeout=10
wait_timeout=86500
interactive_timeout=28800

这些错误是随机发生的,当它针对最初失败的相同输入重试时,它会起作用。

我做错了什么?

【问题讨论】:

  • 考虑 hibernate.c3p0.timeout = "86400";某些数据库服务器配置为关闭太旧的连接。检查您的数据库服务器配置。或者你可以尝试设置一个较低的值。
  • 我已经添加了数据库的系统变量以获得一些信息。

标签: mysql hibernate c3p0 amazon-aurora


【解决方案1】:

解决这个问题的最简单方法(希望如此!)可能是添加...

hibernate.c3p0.testConnectionOnCheckout = "true"

使用您的preferredTestQuery 设置,这不太可能是性能问题。有关连接测试的一些建议,请参阅 here

【讨论】:

    猜你喜欢
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多