Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate not throwing exception when openSession is called and the connection to DB cannot be established

When I configure hibernate to connect to a DB and for some reason the connection fails, I noticed that it doesnt throw any exception neither when building the factory (which is acceptable) nor when I call sessionFactory.openSession() (which is less acceptable). What is more annoying is that session.isConnected() returns true after I call sessionFactory.openSession(). (Which is totally unacceptable).

The only hint up to that moment that tells me that it hasnt connected to the DB is a WARN log entry caused by a java.sql.SQLException that it internally catches and just sends to the logger. Besides creating a pseudo transaction to force an exception is there any other way of getting the connection status? (Maybe an option in the configuration that says "dont log the failed connection attempt, but throw a hibernate exception) . I ve searched but not found anything. Thanks a lot.

like image 433
Paralife Avatar asked Sep 05 '09 10:09

Paralife


1 Answers

What connection pool are you using? Hibernate's built-in pool should not be used for anything other than simple tests.

Proxool, on the other hand, allows you to do exactly what you want: set test-before-use property to true and it will throw an exception if it can't find or create a suitable connection. You can even listen to connection events if you want.

like image 86
ChssPly76 Avatar answered Oct 03 '22 08:10

ChssPly76