我有一个大约 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/