2014-06-17 42 views
0

我在分佈式系統中工作,多個服務器使用前面的MySQL DB,並在不同的各種查詢中複製數據庫。 多臺服務器使用多達1000個連接的化合物(700-800,900次)。C3P0設置不明確

定期我有一個由MySQL關閉的連接問題,看看已經無聊例外:

The last packet sent successfully to the server was 65,179,696 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem. 

WAIT_TIMEOUT MySQL的設置是默認的,等於28800(8小時)

我用C3P0搭配休眠爲服務我到MySQL連接,並在所有休眠定製的* .xml的所有服務器上下一C3P0設置:

<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> 
    <property name="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext</property> 
    <property name="hibernate.connection.url">jdbc:mysql://xxx.xxx.xxx.xxx:3306/ibaserver?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=cp1251&amp;zeroDateTimeBehavior=convertToNull</property> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.username">username</property> 
    <property name="hibernate.connection.password">********</property> 
    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> 

    <property name="hibernate.c3p0.min_size">5</property> 
    <property name="hibernate.c3p0.max_size">50</property> 
    <property name="hibernate.c3p0.timeout">1800</property> 
    <property name="hibernate.c3p0.max_statements">0</property> 
    <property name="hibernate.c3p0.idle_test_period">59</property> 
    <property name="hibernate.c3p0.acquire_increment">3</property> 
    <property name="hibernate.c3p0.preferredTestQuery">select 1</property> 

    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> 
    <property name="hibernate.cache.use_query_cache">false</property> 
    <property name="hibernate.cache.use_second_level_cache">false</property> 

任何人可以回答我做錯了什麼?爲什麼週期性地丟失連接

+0

它看起來就像游泳池返回了一個未使用(閒置)18小時的連接。 'c3p0.timeout'和'c3p0.idle_test_period'設置應該阻止了這一點。你可以驗證這些設置實際上是否按照配置使用? – vanOekel

+0

我應該在connection.url中使用「autoReconnect = true」和c3p0.idle_test_period?如果我只留下其中一個呢? – user2602807

回答

0

對池中的借用使用驗證查詢以避免MySQL關閉連接異常。

UPDATE

定義沒有一些額外的CONFIGS不運行驗證查詢,這些就足夠了: hibernate.c3p0.validate =真 hibernate.c3p0.testConnectionOnCheckout =真

+0

我該怎麼做?現有設置hibernate.c3p0.preferredTestQuery = select 1已經在做,不是嗎? – user2602807

+0

啊哈,我沒注意到。我使用其他池,我懷疑有一些配置丟失,所以這不會真正運行。 – MarianP

+0

hibernate.c3p0.validate = true – MarianP