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 小时后我们仍然遇到 Broken Pipe 问题。 虽然通过谷歌有多个帖子索引,但没有一个得到令人满意的答案。
MySQL seems to have an 8 hour time out on its connections. I'm running multiple WARs in Tomcat utilizing Hibernate for ORM. After 8 hours (i.e. overnight), I get broken pipes when it picks up an idle connection.
I've already traced through the code and made doubly sure I commit or rollback all transactions.
Here is my 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>
The parameter I thought would have fixed it was the c3p0.idle_test_period
-- It defaults to 0. However, we still have the Broken Pipe issue after 8 hours of running. While there are multiple posts index via Google, none arrive at a satisfactory answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
所以事实证明我错过了启用 c3p0 的关键行(我正在调整的 c3p0 参数没有效果,因为 Hibernate 正在使用它内置的连接池——它适当地警告它不适合生产)。 在 hibernate 2.x 中,设置 hibernate.c3p0.max_size 属性启用 c3p0 连接池。 然而,在 3.x 中,您必须指定以下属性 ——
另外,这是我的最终配置参数 ——
不幸的是,Hibernate 和 c3p0 在这方面都有糟糕的文档。
So it turns out I was missing a key line that enabled c3p0 (the c3p0 parameters I was tweaking were having no effect because Hibernate was using it's built in connection pool -- which it appropriately warns is not suitable for production). In hibernate 2.x, setting the
hibernate.c3p0.max_size
property enabled c3p0 connection pooling. However, in 3.x you must specify the following property --Additionally, here are my final configuration parameters --
It's rather unfortunate that both Hibernate and c3p0 have abysmal documentation in this regard.
这里发生了两件事。 您应该阅读这篇文章了解更多详细信息,但要点是:
hibernate.c3p0.idle_test_period
而不仅仅是c3p0.idle_test_period
There are two things going on here. You should read this article for more details, but the take-aways are:
hibernate.c3p0.idle_test_period
instead of justc3p0.idle_test_period
这是当你因 c3p0 中 tomcat 的 wait_timeout=28800 sec(8h) 和 maxIdleTime=0 的组合而导致管道损坏时的解决方案:
我已通过 my.ini 文件将本地 tomcat wait_timeout 更改为 120 秒(2 分钟)。 我放置了以下内容:
最大空闲时间=100
idleConnectionTestPeriod=0(与默认值相同/就好像它不存在一样)
其他:
获取增量=2
minPoolSize=2
最大池大小=5
maxIdleTimeExcessConnections=10
我对这个设置没有任何问题。
我不需要使用idleConnectionTestPeriod!
如果tomcat的wait_timeout是28800秒,maxIdleTime是25200,这意味着c3p0会在tomcat抛出“broken pipeline”异常之前提前3600秒(1h)关闭空闲连接。 是不是这样啊?!
正如您所看到的,我对仅提供 maxIdleTime 没有任何问题。
不幸的是,这些:
maxIdleTime
idleConnectionTestPeriod
configuring_connection_testing
testConnectionOnCheckin
不要过多解释极端情况。
顺便说一句,以下是如何使用 Notepad++ 打开 tomcat 的 my.ini 文件:
http://drupal.org/node/32715#comment-4907440
干杯,
暴君
This is a solution when you have a broken pipe because of combination of tomcat's wait_timeout=28800 sec(8h) and maxIdleTime=0 in c3p0:
I've changed the local tomcat wait_timeout via my.ini file to 120sec (2 min). And I placed the following:
maxIdleTime=100
idleConnectionTestPeriod=0 (same as default/as if it didn't exist)
other:
acquireIncrement=2
minPoolSize=2
maxPoolSize=5
maxIdleTimeExcessConnections=10
I had no problems with this setup.
I didn't need to use idleConnectionTestPeriod!
If tomcat's wait_timeout is 28800 sec, and maxIdleTime is 25200, it means that c3p0 will close the idle connection in 3600sec (1h) earlier, before tomcat throws a "broken pipe" exception. Isn't that right?!
As you can see I have no issues with providing only maxIdleTime.
Unfortunately, these:
maxIdleTime
idleConnectionTestPeriod
configuring_connection_testing
testConnectionOnCheckin
don't explain too much the corner cases.
And, btw, here is how to open the tomcat's my.ini file with Notepad++:
http://drupal.org/node/32715#comment-4907440
Cheers,
Despot
我有几个问题 -
- 未找到 C3P0ConnectionProvider
- 我通过使用 hibernate c3p0 版本解决它
- 我在 mysql 上遇到 wait_timeout 问题。 首先我设置 /etc/my.cnf wait_timeout=10
然后我将空闲超时值更改为低于 wait_timeout 值,该值 < 10
这解决了我的问题。
I've several problem -
- C3P0ConnectionProvider was not found
- I solve it by using the hibernate c3p0 version
- I have that wait_timeout issue on mysql. First I set /etc/my.cnf wait_timeout=10
then I changed the Idle time out value to lower than the wait_timeout value which < 10
That solved my problem.
我遇到了同样的问题,花了一些时间才找到解决方案。
我使用 Hibernate 4.0.1 和 mysql 5.1(没有 spring 框架),我遇到了这个问题。 首先确保您正确配置了 c3p0 jar,这是必不可少的。
我在 hibernate.cfg.xml 中使用了这些属性
,但它没有用,因为 C3p0 仍然采用默认属性而不是我在 hibernate.cfg.xml 中设置的属性,您可以在日志中检查它。 因此,我在很多网站上搜索了正确的解决方案,最后我想出了这个。 删除 cfg.xml 中的 C3p0 属性,并在根路径中创建 c3p0-config.xml(以及 cfg.xml),并按如下方式设置属性。
但如果你运行,ORM 采用 jdbc 连接而不是 C3p0 连接池,因为我们应该在 hibernate.cfg.xml 中添加这些属性,
现在一切正常(至少对我来说工作正常)并且问题得到解决。
检查以下内容以获取参考。
http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing
https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool
我希望这能解决你的问题。
I was getting the same problem and it took time to figure out the solution.
I use Hibernate 4.0.1 and mysql 5.1(no spring framework) and I was facing the issue. First make sure that you configured the c3p0 jars properly which are essential.
I used these properties in hibernate.cfg.xml
But it's of no use 'cause C3p0 was still taking the default properties not the properties which I set in hibernate.cfg.xml, You can check it in logs. So, I searched many websites for right solution and finally I came up with this. remove the C3p0 properties in cfg.xml and create c3p0-config.xml in the root path(along with cfg.xml) and set properties as follows.
but if you run, ORM takes the jdbc connection but not C3p0 connection pool 'cause we should add these properties in hibernate.cfg.xml
now everything works fine(At least it worked fine for me) and the issue is solved.
check the following for references.
http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing
https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool
I hope this solves your problem.