通过数据库断开连接

时间:2018-11-12 12:39:10

标签: mysql hibernate c3p0 amazon-rds-aurora

我的系统约有20 TPS。该系统建立数据库连接以获取一些数据。我正在使用带有c3p0的Hibernate来管理我的连接,并使用@transactional注释来确保Guice的可交易性。 由于某些数据库连接问题,将近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

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

我在做什么错?

1 个答案:

答案 0 :(得分:0)

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

hibernate.c3p0.testConnectionOnCheckout = "true"

设置好preferredTestQuery后,就不太可能出现性能问题。有关连接测试的一些建议,请参见here