15

MySQL 的连接似乎有 8 小时的超时时间。我正在使用 Hibernate for ORM 在 Tomcat 中运行多个 WAR。8 小时后(即一夜之间),当它获得空闲连接时,我的管道损坏了。

我已经跟踪了代码,并加倍确定我提交或回滚了所有事务。

这是我的 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 小时后,我们仍然遇到 Broken Pipe 问题。虽然谷歌有多个帖子索引,但没有一个得到令人满意的答案。

4

5 回答 5

25

所以事实证明我错过了启用 c3p0 的关键行(我正在调整的 c3p0 参数没有效果,因为 Hibernate 正在使用它内置的连接池——它适当地警告它不适合生产)。在 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 在这方面都有糟糕的文档。

于 2009-04-10T16:09:09.227 回答
2

这里有两件事。您应该阅读这篇文章以了解更多详细信息,但要点是:

  1. 如果需要,您可以将 MySQL wait_timeout设置调整为大于 8 小时。
  2. 休眠设置应包括“休眠”。在“c3p0”之前,例如,hibernate.c3p0.idle_test_period而不仅仅是c3p0.idle_test_period
于 2009-04-08T21:39:24.497 回答
2

这是由于 c3p0 中的 tomcat 的 wait_timeout=28800 sec(8h) 和 maxIdleTime=0 的组合而导致管道损坏时的解决方案:

我已通过 my.ini 文件将本地 tomcat wait_timeout 更改为 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
configure_connection_testing
testConnectionOnCheckin
并没有解释太多极端情况。

而且,顺便说一句,这里是如何使用 Notepad++ 打开 tomcat 的 my.ini 文件:http: //drupal.org/node/32715#comment-4907440

干杯,
暴君

于 2012-09-18T15:05:27.507 回答
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 然后我将空闲超时值更改为低于 <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;"/>
于 2012-04-20T16:58:48.093 回答
1

我遇到了同样的问题,需要时间来找出解决方案。

我使用 Hibernate 4.0.1 和 mysql 5.1(没有 spring 框架),我遇到了这个问题。首先确保您正确配置了 c3p0 jar,这是必不可少的。

我在 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 中设置的属性,您可以在日志中检查它。所以,我在很多网站上搜索了正确的解决方案,最后我想出了这个。删除 cfg.xml 中的 C3p0 属性并在根路径(以及 cfg.xml)中创建 c3p0-config.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

我希望这能解决你的问题。

于 2013-06-12T10:24:48.047 回答