【发布时间】:2012-05-13 01:44:07
【问题描述】:
我正在使用带有 Hibernate 的 C3P0 连接池来执行一些 JDBC 操作。但是,使用一段时间后,我收到“关闭连接”(SQL 错误:17008,SQLState:null)错误。
我正在使用org.hibernate.jdbc.Work 接口来执行我的操作:
public class ClassThatDoesWork implements Work {
@Override
public void execute(final Connection connection)
throws SQLException {
doSomeWork();
//should connection be closed here?
}
}
我的问题是:作为参数传递给 execute() 方法的 connection 对象应该在该方法结束时关闭,还是 Hibernate 会自动处理?
编辑 这些是使用的 Hibernate 和 c3p0 参数:
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.pool_size=10
hibernate.dialect=org.hibernate.dialect.Oracle9iDialect
hibernate.connection.provider_class=org.hibernate.connection.C3P0ConnectionProvider
hibernate.show_sql=false
acquireIncrement=3
acquireRetryDelay=500
acquireRetryAttempts=5
breakAfterAcquireFailure=false
checkoutTimeout=0
connectionTesterClassName=com.mchange.v2.impl.DefaultConnectionTester
debugUnreturnedConnectionStackTraces=false
dataSourceName=irrelevantDB
identityToken=irrelevantDB
idleConnectionTestPeriod=0
initialPoolSize=3
maxConnectionAge=0
maxIdleTime=7200
maxIdleTimeExcessConnections=0
maxPoolSize=20
maxStatements=50
maxStatementsPerConnection=0
minPoolSize=5
numHelperThreads=3
propertyCycle=0
testConnectionOnCheckin=false
testConnectionOnCheckout=true
unreturnedConnectionTimeout=0
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=10
hibernate.c3p0.max_statements=50
【问题讨论】:
-
每次我使用 C3P0 时,它的设置都是在没有 hibernate 前缀的情况下指定的。 IE。 c3p0.min_size=5 而不是 hibernate.c3p0.min_size=5。请根据我的回答尝试 c3p0.idle_test_period 设置。
-
请参阅this part of the c3p0 documentation。属性确实以“hibernate.c3p0”为前缀。一旦我尝试了 c3p0.idle_test_period 设置,我就会回来。
-
抱歉,我们根本没有使用 C3P0,因为它没有被正确地作为依赖项,所以这可能会解决“连接关闭”问题,不管 c3p0 是什么。 idle_test_period 属性。但是,如果
connection对象应该由我的代码维护或 Hibernate 自动处理它,我仍然感兴趣。 -
Hibernate 作为方法参数传入,因此不应在方法内部进行诱惑(例如关闭)——这是 Hibernate 的职责。
-
谢谢!请在您的回答中说明这一点,以便我接受。