2010-08-11 179 views
0

我使用休眠的MySQLC3P0的java.io.EOFException的,並且當8小時(wait_timeout)之後的MySQL連接期滿,Hibernate是無法重新連接而我得到以下異常:休眠,C3P0和MySQL:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 
** BEGIN NESTED EXCEPTION ** 

java.io.EOFException 

STACKTRACE: 

java.io.EOFException 
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1963) 
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2375) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2874) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715) 
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3249) 
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268) 
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1403) 
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76) 
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208) 
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1812) 
    at org.hibernate.loader.Loader.doQuery(Loader.java:697) 
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) 
    at org.hibernate.loader.Loader.doList(Loader.java:2232) 
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129) 
    at org.hibernate.loader.Loader.list(Loader.java:2124) 
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363) 
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196) 
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149) 
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) 

這是hibernate.cfg:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 

     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property> 
     <property name="hibernate.connection.username">username</property> 
     <property name="hibernate.connection.password">password</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> 
     <property name="hibernate.query.substitutions">true 1, false 0</property> 

     <property name="hibernate.current_session_context_class">thread</property> 

     <property name="hibernate.hbm2ddl.auto">validate</property> 
     <property name="hibernate.connection.autocommit">false</property> 

     <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>   
     <property name="hibernate.c3p0.min_size">3</property> 
     <property name="hibernate.c3p0.max_size">10</property> 
     <property name="hibernate.c3p0.timeout">180</property> 
     <property name="hibernate.c3po.max_idle_time">180</property> 
     <property name="hibernate.c3p0.idle_test_period">300</property> 
     <property name="hibernate.c3p0.acquire_increment">3</property> 

     <!-- My mappings here --> 
    </session-factory> 
</hibernate-configuration> 

,這是我c3p0.properties文件:

c3p0.preferredTestQuery=SELECT 1 

UPDATE:

下面的日誌文件:

INFO - com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getPoolManager(462) | Initializing c3p0 pool... [email protected] 
c [ connectionPoolDataSource -> [email protected] [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetr 
yDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassNam 
e -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation 
-> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> jbnj238aq9q7lecas8ck|79b0d33c, idleConnectionTestPeriod -> 300, initialPoolSize -> 3, 
maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 180, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 0, maxState 
mentsPerConnection -> 0, minPoolSize -> 3, nestedDataSource -> [email protected] [ description -> null, driverClass -> null 
, factoryClassLocation -> null, identityToken -> jbnj238aq9q7lecas8ck|445ead9d, jdbcUrl -> jdbc:mysql://localhost:3306/mydb, properties -> {user=******, p 
assword=******, autocommit=false} ], preferredTestQuery -> SELECT 1, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, 
unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, iden 
tityToken -> jbnj238aq9q7lecas8ck|26796e1c, numHelperThreads -> 
3 ] 

任何尖端被理解。
謝謝

+0

你可以包含c3p0初始化日誌輸出嗎?例如,在我的機器上,我看到'Initializing c3p0 pool ... [email protected] [connectionPoolDataSource - > [email protected] [acquireIncrement - > 3,acquireRetryAttempts - > 30'等,列出完整的配置。這是驗證您的設置正在被拾取的好方法。 – 2010-08-11 18:56:23

+0

當然,我已經更新了我的問題。 – Mark 2010-08-11 20:18:36

回答

0

這是我抓到的部分。

the MySQL connection expires after 8 hours 

如果您使用c3p0,則不應該發生這種情況。你應該建立一個新的連接並將它們存儲在池中。

如果您不添加新連接或刷新那些連接,您將超時。如果您多次嘗試超時,請告訴c3p0重建/刷新連接池。

-1

愚蠢的問題:在Hibernate的配置,你有下面這行:

<property name="hibernate.c3po.max_idle_time">180</property> 

oc3po應該是一個0。這只是一個複製和粘貼錯誤?丟失max_idle_time設置肯定會導致這個問題。

編輯:在審查c3p0 docs後,它看起來像正確的設置名稱是hibernate.c3p0.timeout

編輯2:我只是看着你的配置,你已經有hibernate.c3p0.timeout。 D'哦。所以你可以忽略這整個答案:-P。

1

我解決了這個問題:

設置C3P0屬性時,的hibernate.c3p0.idle_test_period值一定不能是hibernate.c3p0.timeout高。所以在我的情況設置:

<property name="hibernate.c3p0.timeout">180</property> 
<property name="hibernate.c3p0.max_idle_time">180</property> 
<property name="hibernate.c3p0.idle_test_period">100</property> 

修復它。