0

我们使用 HibernateORM 为我们的 mysql 数据库使用 C3p0 连接池。

以下是 hibernate.cfg.xml 中的设置

<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.maxConnectionAge">3600</property>
<property name="hibernate.c3p0.timeout">120</property>
<property name="hibernate.c3p0.max_size">300</property>
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.preferredTestQuery">select 1;</property>

为了在 28800 秒后重新连接到数据库,我们设置了参数 -

c3p0.testConnectionOnCheckout=true

但是我们面临着休眠异常。

因此,如果我现在调用 DB 并等待 8 小时(或者我设置 my.cnf 的变量 wait_timeout 的任何时间),如果我再次调用 DB ,我会得到该异常。堆栈跟踪 -

org.hibernate.exception.JDBCConnectionException: could not execute query
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.loader.Loader.doList(Loader.java:2536)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
        at org.hibernate.loader.Loader.list(Loader.java:2271)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:452)
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)

作为该问题的快速解决方案,我们每天早上都会重新启动应用服务器。

任何帮助,将不胜感激

- 谢谢

4

1 回答 1

0

if your config is what you think it is, there's no way these Exceptions come from stale Connections getting checked out after sitting idle and timing out in the pool. Connections expire after an hour no matter what, they are never idle more than about five minutes, they are tested on checkout.

one of two things is going on:

1) you don't have the configuration you think you have, something is going wrong in the middlewhere between your config and the c3p0 DataSource it intedns to configure.

fortunately, c3p0 DataSources dump their config at INFO level on initialization. check your logs, and verify that your pool has the config that you intend it to have.

2) perhaps your app is holding Connections live outside of the pool, rather than checking them out for short periods and close()ing them, so that c3p0 ca do all that testing, expiring, etc. if your app holds Connections for so long that they time out, there is nothing c3p0 can do about it. c3p0 can help you test if this is what's going on, though. try the config parameters unreturnedConnectionTimeout and debugUnreturnedConnectionStackTraces. Use them together. See

http://www.mchange.com/projects/c3p0/#configuring_to_debug_and_workaround_broken_clients

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

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

good luck!

于 2012-12-14T09:33:05.813 回答