mysql - 连接被数据库丢弃

标签 mysql hibernate c3p0 amazon-aurora

我有一个大约 20 TPS 的系统。该系统建立数据库连接以获取一些数据。我使用 Hibernate 和 c3p0 来管理我的连接,并使用 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

我的 hibernate 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.testConnectionOnCheckout = "true"

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

关于mysql - 连接被数据库丢弃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53262379/

相关文章:

java - 如何将更新的实体与旧实体进行比较并仅将更新的值存储在数据库中

java - autoCommit var 在关闭时是否在 c3p0 中重置?

spring - 配置 Hibernate C3P0 连接池

Mysql:添加外键不会在 MyISAM 表上给出警告/错误

MySQL SUM、INNER JOIN 和 GROUP BY 在同一条语句中。语法错误

php - MySQL PHP 在没有我输入的情况下添加 3306

Java C3P0 池化数据库连接导致 "Unable to obtain storage for thread control block"

php - 将 PHP 表单插入到 MySQL 数据库会产生重复的意外结果

java - 从一个 pojo 映射多个表

java - 提交后是否禁止 begin() 同一事务(事务重用)?