Hibernation does not recover after the database bounces

I found that after we bounced (stopped and started) our database (postgresql 8.3), our applications that use hibernate (3.2.6) cannot reconnect, instead they get a SocketException with a "broken pipe" message .

I believe that we are configured to use the built-in connection pool.

how can i get hibernate to re-acquire connections after restarting db without restarting the application?

R.

+7
source share
2 answers

What you want is a feature called connection check provided by connection pools. The connection pool should run a quick request to make sure that the connection it is about to transfer is not obsolete. Unfortunately DriverManagerConnectionProvider , the Hibernate default pooling class, does not support this function. The hibernate command strongly discourages the use of this pool of connections in production code.

Hibernate's connection pooling algorithm, however, is in its infancy. It is intended to help you get started and are not going to be used in a production system or even for performance testing. You should use a third-party pool for performance and stability.

My recommendation is that you switch to a different connection pool implementation. If you upgrade to C3P0 (which comes with Hibernate), connection testing can be configured as described here . If you are using Apache DBCP, it allows you to set validationQuery as described here .

+7
source

If you use hibernate in jboss, you can configure it in the * -ds.xml file in the deployment directory to execute some query before connecting to the connection pool: For example:

<check-valid-connection-sql>select 1 from dual</check-valid-connection-sql> 

If you use your own connection pool, you can write something like this:

 Connection getConnectionFromPool() { try { //get connection from pool //execute some simple uery that should always work } catch (SocketException s) { //close broken connection and get a new one } return connection; } 

Or maybe there is a mechanism for doing this in sleep mode, but I don't know about that.

EDIT: Ok, after reading your question, I skipped that you are using the sleep connection pool. So ignore my answer and look here: Restore hibernation

+2
source

All Articles