Search code examples
javahibernateconnectionc3p0sessionfactory

How to make Hibernate SessionFactory checkout JDBC connection from c3p0 upon session opening?


This would be useful because I want to know if the pool is exhausted BEFORE I need to create a Transaction from Session in my DAOs.

I would like to give feedback to the user about system overload (pool exhaustion), but I don't want to capture this specific Exception in every method of every DAO. I think Session opening would be the right time to check this.


Solution

  • Newer versions of Hibernate have this Database connection property:

     hibernate.connection.acquisition_mode
    

    which can be set to IMMEDIATELY, thus

    "The Connection will be acquired as soon as the Hibernate Session is opened. This also circumvents ConnectionReleaseMode, as the Connection will then be held until the Session is closed."