I\'m developing with Java SE application using Hibernate 4 and c3p0 for communication with MariaDB database. It\'s long running application, waiting for signals from outside, so
Ok, i managed to fix all problems. Here is soultion.
First of all, as Steve Waldman suggested, c3p0 wasn't actually initialized, but in Hibernate 4.3 hibernate.connection.provider_class parameter should be: org.hibernate.c3p0.internal.C3P0ConnectionProvider. In documentation you can read:
A connection provider that uses a C3P0 connection pool. Hibernate will use this by default if the hibernate.c3p0.* properties are set.
But in my opinion it's good to put this parameter by yourself, and it's needed if you are using c3p0 config file, not hibernate.c3p0.* properties.
Second problem was obtaining session by calling SessionFactory.openSession() at the begining of the thread and later using always the same session object. I suppose after database connection was broken and there was new one recreated in pool, using old session caused using old, broken connection. So possible solutions were obtaining new session by SessionFactory.openSession() after catching connection error or using SessionFactory.getCurrentSession() at the begining of every communication. I've decided to use the second option (in this case my application waits for external signal, sometimes for hours, so I'm obtaining current session after each signal).
My final configuration:
hibernate.cfg.xml:
org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/MyBase?zeroDateTimeBehavior=convertToNull&autoReconnect=true
user
pass
org.hibernate.c3p0.internal.C3P0ConnectionProvider
thread
true
true
true
true
org.hibernate.cache.ehcache.EhCacheRegionFactory
c3p0-config.xml:
5
5
10
3000
30
SELECT 1 FROM DUAL
true
false
300
With this c3p0 configuration:
HibernateException will be thrown, then connection will be reestablished and next query will succeed.Optionally testConnectionOnCheckout could be set to true to prevent exceptions, but it will cause performance troubles (see documentation).