Question

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.

Was it helpful?

Solution 2

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."

OTHER TIPS

Hibernate does it best to use a database connection as short as possible. Claiming a database connection when a session is opened goes against this (there are no queries available to perform, this will come later). A side effect would be that the pool is exhausted much sooner.

c3p0 uses a checkoutTimeout to determine how long to wait for a connection to become available when the pool is exhausted. By default this timeout is 0 (i.e. no timeout) but if it is set to another value (e.g. 50 000 milliseconds) and the timeout is reached, a SQLException is thrown.

You can try to make an educated guess to see if the checkoutTimeout will be reached before you start a transaction, but it will always be a guess: it depends on how fast the other transactions complete.
To make the guess you could use c3p0's JMX interface and get the values for the number of connections (NumConnections), the number of busy connections (NumBusyConnections) and the number of threads waiting for a connection (NumThreadsAwaitingCheckout).
If there are many busy connections and many threads are waiting, there is a good chance the checkoutTimeout will be reached, but you'll have to determine the exact numbers via stress-testing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top