2009-04-08 56 views
15

MySQL的連接似乎有8小時超時。我在Tomcat中使用Hibernate for ORM運行多個WAR。 8小時後(即過夜),當我拿起空閒的連接時,我會斷開管道。Hibernate,C3P0,Mysql - Broken Pipe

我已經通過代碼追蹤了兩次,並確認我提交或回滾所有事務。

這裏是我的hibernate.cfg.xml

<?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.bytecode.use_reflection_optimizer">false</property> 
    <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property> 
    <property name="hibernate.connection.password"></property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost/test</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 
    <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <!--property name="hibernate.show_sql">true</property> 
    <property name="hibernate.format_sql">true</property--> 

    <property name="c3p0.min_size">3</property> 
    <property name="c3p0.max_size">5</property> 
    <property name="c3p0.timeout">1800</property> 
    <property name="c3p0.preferredTestQuery">SELECT 1</property> 
    <property name="c3p0.testConnectionOnCheckout">true</property> 
    <property name="c3p0.idle_test_period">100</property> <!-- seconds --> 

    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
    <property name="cache.use_query_cache">false</property> 
    <property name="cache.use_minimal_puts">false</property> 
    <property name="max_fetch_depth">10</property> 

    <property name="hibernate.hbm2ddl.auto">update</property> 

    <!-- classes removed --> 

</session-factory> 

我以爲該參數將有固定它是c3p0.idle_test_period - 它默認爲0。但是,我們仍然有破裂的管道問題跑完8小時後。雖然Google有多個帖子索引,但沒有一個能得到滿意的答案。

回答

23

因此,原來我是缺少啓用C3P0(一鍵行C3P0參數我被調整,因爲Hibernate使用它內置的連接池都沒有效果 - 它適當的警告不適合生產)。在休眠2.x中,設置hibernate.c3p0.max_size屬性啓用了c3p0連接池。然而,在3.x中必須指定以下屬性 - 此外

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 

,這裏是我的最終配置參數 -

<property name="hibernate.c3p0.min_size">3</property> 
<property name="hibernate.c3p0.max_size">5</property> 
<property name="hibernate.c3p0.timeout">1800</property> 
<property name="hibernate.c3p0.idle_test_period">100</property> <!-- seconds --> 

這是相當不幸的是,Hibernate和C3P0都有深不可測的文檔中對此。

+0

謝謝,這對我有幫助。我同意文檔評論。更糟糕的是,c3p0文檔建議您將c3p0.properties用於除上述5之外的其他所有其他用戶,但這不起作用。只有當你在沒有hibernate前綴的情況下在persistence.xml中指定時纔有效(如你在原始cfg.xml中所做的那樣) – Sun 2010-07-16 15:58:46

2

這裏有兩件事情。你應該閱讀this article更多的細節,但外賣是:

  1. 您可以調整MySQL的wait_timeout設置超過8小時更大的東西,如果需要的話。
  2. 休眠設置應該包括「休眠」。在「c3p0」之前,例如hibernate.c3p0.idle_test_period而不是僅僅c3p0.idle_test_period
+0

我會嘗試今天添加hibernate前綴。我不認爲延長MySQL wait_timeout會有幫助...我只是推遲了破損的管道錯誤。 – Mark 2009-04-10 14:11:01

1

我有幾個問題 - - C3P0ConnectionProvider未發現 - 我解決它通過使用休眠C3P0版本

 <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>3.5.6-Final</version> 
    </dependency> 
      <!-- c3p0 --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-c3p0</artifactId> 
     <version>3.3.1.GA</version> 
    </dependency> 

- 我有一個在MySQL WAIT_TIMEOUT問題。首先,我設置了/etc/my.cnf wait_timeout = 10 然後我將空閒超時值更改爲低於wait_timeout值,這就解決了我的問題。

<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" /> <property name="hibernate.c3p0.acquire_increment" value="1" /> 
     <property name="hibernate.c3p0.idle_test_period" value="28690"/> 
     <property name="hibernate.c3p0.timeout" value="1800" /> 
     <property name="hibernate.c3p0.max_size" value="5" /> 
     <property name="hibernate.c3p0.min_size" value="3" /> 
     <property name="hibernate.c3p0.max_statement" value="50" /> 
     <property name="hibernate.c3p0.preferredTestQuery" value="select 1;"/> 
2

這是一個解決方案,當你有因爲C3P0 tomcat的WAIT_TIMEOUT = 28800秒(8小時)和maxIdleTime = 0的組合,一個破裂的管道:

我已經改變了本地的Tomcat通過WAIT_TIMEOUT我.ini文件到120秒(2分鐘)。我放入下列:
maxIdleTime = 100
idleConnectionTestPeriod = 0(與默認/彷彿它不存在)
其他:
acquireIncrement = 2
了MinPoolSize = 2
maxPoolSize = 5
maxIdleTimeExcessConnections = 10

我對此設置沒有任何問題。

我不需要使用idleConnectionTestPeriod!

如果tomcat的wait_timeout爲28800秒,maxIdleTime爲25200,則意味着c3p0將在3600sec(1h)之前關閉空閒連接,之後tomcat會拋出「斷開的管道」異常。不是嗎?!

正如你所看到的,我只提供maxIdleTime沒有問題。

不幸的是,這些:
maxIdleTime
idleConnectionTestPeriod
configuring_connection_testing
testConnectionOnCheckin
不會解釋太多的極端案例。

而且,順便說一句,這裏是如何打開tomcat的用記事本++ my.ini文件: http://drupal.org/node/32715#comment-4907440

乾杯,
暴君

1

我得到了同樣的問題,它需要時間來弄清楚解決方案。

我使用Hibernate 4.0.1和MySQL 5.1(無彈簧框架)和我面臨的問題。首先確保您正確配置了c3p0罐子,這是必不可少的。

我在hibernate.cfg.xml

<property name="hibernate.c3p0.validate">true</property> 
<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property> 
<property name="hibernate.c3p0.min_size">5</property> 
<property name="hibernate.c3p0.max_size">20</property> 
<property name="hibernate.c3p0.max_statements">50</property> 
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property> 
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property> 
<property name="hibernate.c3p0.idle_test_period">10</property> 
<property name="hibernate.c3p0.acquireRetryAttempts">5</property> 
<property name="hibernate.c3p0.acquireRetryDelay">200</property> 
<property name="hibernate.c3p0.timeout">40</property> 

使用這些屬性,但它是沒有用的「事業C3P0依然在採取默認屬性不是我在hibernate.cfg.xml設置的屬性,你可以在日誌中檢查它。所以,我搜索了很多網站的正確解決方案,最後我想出了這個。除去C3P0性質cfg.xml中,並創建在根路徑C3P0-config.xml中(與cfg.xml中一起)並設置屬性如下。

<c3p0-config> 
<default-config> 
<property name="automaticTestTable">con_test</property> 
<property name="checkoutTimeout">40</property> 
<property name="idleConnectionTestPeriod">10</property> 
<property name="initialPoolSize">10</property> 
<property name="maxPoolSize">20</property> 
<property name="minPoolSize">5</property> 
<property name="maxStatements">50</property> 
<property name="preferredTestQuery">SELECT 1;</property> 
<property name="acquireRetryAttempts">5</property> 
<property name="acquireRetryDelay">200</property> 
<property name="maxIdleTime">30</property> 
</default-config> 
</c3p0-config> 

你要是跑不過,ORM採用JDBC連接而不是C3P0連接池,因爲我們要在hibernate.cfg.xml中添加這些屬性

<property name="hibernate.c3p0.validate">true</property> 

<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property> 

現在一切正常(至少它爲我工作得很好),問題解決了。

檢查涉及以下。

http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing

https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool

我希望這能解決你的問題。

相關問題