是否应该关闭doWork()的“连接”参数?

时间:2012-05-03 14:09:23

标签: java hibernate jdbc c3p0

我正在使用带有Hibernate的C3P0连接池来执行一些JDBC操作。但是,在使用一段时间后,我收到“Closed Connection”(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?
    }
}

我的问题是:作为connection方法的参数传递的execute()对象是否应该在该方法结束时关闭,或者Hibernate会自动关注它?

EDIT 这些是使用的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

1 个答案:

答案 0 :(得分:8)

数据库连接作为Hibernate的方法参数传入,因此不应该在方法内部(例如关闭)进行诱惑 - 这是Hibernate的职责。