Hibernate、C3P0、Mysql — 损坏的管道
题
MySQL 的连接超时时间似乎为 8 小时。我在 Tomcat 中使用 Hibernate 进行 ORM 运行多个 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小时后,我们仍然遇到断管问题。虽然通过谷歌有多个帖子索引,但没有一个得到令人满意的答案。
解决方案
因此,原来我是缺少启用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两者在这方面深不可测文档。
其他提示
这是一个解决方案,当你有因为Tomcat的WAIT_TIMEOUT = 28800秒(8H)和maxIdleTime = 0 C3P0的组合的一个断裂的管道:
我已经改变了本地的Tomcat经由my.ini文件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 结果 configuring_connection_testing 结果 testConnectionOnCheckin 结果 不用解释太多的极端案例。
和,顺便说一句,这里是如何打开Tomcat的my.ini文件,用记事本++: http://drupal.org/node/32715#comment-4907440
干杯,结果 霸
我有几个问题 - - 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>
- 我有WAIT_TIMEOUT问题上的MySQL。首先,我的/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;"/>
我得到同样的问题,需要时间来找出该溶液中。
我使用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
我希望这能解决你的问题。