2012-05-03 61 views
7

我在Hibernate中使用C3P0連接池來執行一些JDBC操作。但是,在使用一段時間後,出現「Closed Connection」(SQL錯誤:17008,SQLState:null)錯誤。應該關閉doWork()的「連接」參數嗎?

我使用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對象在該方法的年底前關閉或休眠照顧自動?

EDIT 這些是用於休眠和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 
+0

每次使用C3P0時,都會指定其設置而不使用休眠前綴。即c3p0.min_size = 5而不是hibernate.c3p0.min_size = 5。請按照我的答案嘗試c3p0.idle_test_period設置。 – 01es

+0

請參閱[這部分c3p0文檔](http://www.mchange.com/projects/c3p0/index.html#hibernate-specific)。屬性的前綴確實是「hibernate.c3p0」。一旦我嘗試c3p0.idle_test_period設置,我會回來。 – nekojsi

+0

對不起,事實證明,我們根本沒有使用C3P0,因爲它沒有正確地作爲依賴關係被拾取,因此無論c3p0.idle_test_period屬性如何,這可能會解決「連接已關閉」問題。然而,如果連接對象應該由我的代碼維護,或者Hibernate自動處理它,我仍然感興趣。 – nekojsi

回答

8

數據庫連接傳遞在由休眠的方法參數,因此,不應該與被誘惑(例如關閉)方法內 - - 這是Hibernate的責任。

相關問題