简体   繁体   中英

Hibernate JDBCConnectionException - C3p0 connection pooling

We are using C3p0 connection pooling for our mysql db using HibernateORM.

Below are the settings in 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>

And for reconnection to the database after 28800 seconds we set the params -

c3p0.testConnectionOnCheckout=true

But we are facing hibernate exceptions.

So if I make a call to the DB now and wait for 8 hours ( or whatever amount of time I set the variable wait_timeout of my.cnf ), I get that exception if I make another call to the DB . stacktrace -

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)

As a quick fix to the issue, we are restarting the app server every day morning.

Any help would be appreciated

-- Thanks

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM