When I start my application I test my internal database is okay as follows:
HibernateUtil.beginTransaction();
This is a H2 database. I’m using Hibernate on top with c3p0 pooling.
Trouble is it takes one minute to decide that it cannot being a transaction, I’d like it to retry within 10 seconds. I’ve looked at c3p0 properties and cannot see anything that does this.
* Update *
Comment made me realize that HibernateUtil.beginTransaction() was hiding what I was doing so replaced with:
System.out.println("Get Session"+new Date());
Session session = com.jthink.songlayer.hibernate.HibernateUtil.getSession();
System.out.println("Get Txn"+new Date());
Transaction t = session.getTransaction();
System.out.println("Set Timeout"+new Date());
t.setTimeout(10);
System.out.println("Begin Txn"+new Date());
session.beginTransaction();
System.out.println("Commit"+new Date());
t.commit();
Output is
Get SessionWed Nov 21 12:15:56 GMT 2012
Get TxnWed Nov 21 12:16:27 GMT 2012
Set TimeoutWed Nov 21 12:16:27 GMT 2012
Begin TxnWed Nov 21 12:16:27 GMT 2012
FailedWed Nov 21 12:16:46 GMT 2012
So it seems 30 seconds is spent getting a Session (unclear how it can even get a Session as the database is corrupt) and then 19 seconds trying to begin transaction
whereas when the database is fine, the timings are a few seconds
Get SessionWed Nov 21 12:23:57 GMT 2012
Get TxnWed Nov 21 12:23:59 GMT 2012
Set TimeoutWed Nov 21 12:23:59 GMT 2012
Begin TxnWed Nov 21 12:23:59 GMT 2012
CommitWed Nov 21 12:23:59 GMT 2012
So the main problem seems to be that it takes a longtime to (sort of) get a Session when db corrupt
I suppose you didn’t configure
acquireRetryAttemptsfor your C3P0 and it tries to connect to the database for 30 times which is the default value. Decrease this value and you’ll get it much faster.Also there are options you might want to tune:
acquireRetryDelay(default: 1s)breakAfterAcquireFailure(default: false)