Restore hibernation

Does anyone know how to restore / redo sleep mode. I mean, for example: the remote database is disconnected, and I start my application. Hibernate cannot establish a connection. he does not work. But the application is not closed. Is there any way to say that sleep mode is trying to establish a connection again?

Thanks in advance

+6
source share
3 answers

You really have to go to the C3P0 connection pool: http://www.mchange.com/projects/c3p0/index.html#hibernate-specific

The C3P0 documentation on this subject has a section: http://www.mchange.com/projects/c3p0/index.html#configuring_recovery

First you need to configure c3p0 correctly, which, if you use hibernate, should happen in the c3p0.properties file.

In your c3p0.properties put these properties to reconnect indefinitely every 3 seconds when the database is down:

c3p0.acquireRetryAttempts = 0 c3p0.acquireRetryDelay = 3000 c3p0.breakAfterAcquireFailure = false 

In addition, in order not to damage the connections lying in your pool indefinitely, use the connection age control:

 c3p0.maxConnectionAge = 6000 c3p0.maxIdleTime = 6000 c3p0.maxIdleTimeExcessConnections = 1800 c3p0.idleConnectionTestPeriod = 3600 

It can be quite expensive, but useful if the above is not enough:

 c3p0.testConnectionOnCheckout = true c3p0.preferredTestQuery = SELECT 1; 

You can also check for leaks in connections that prevent recovery:

 c3p0.debugUnreturnedConnectionStackTraces = true 

Finally, make sure that C3P0 is connected correctly to hibernate, enable debugging logging for the com.mchange package and see if C3P0 notifies itself. It should contain the configuration properties that are loading, so see if this is all.

Hope this helps.

+18
source

C3P0 is an implementation of the internal connection pool for sleep mode.

Add "hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider" in the hibernation properties file. Create the c3p0.properties file by setting the parameters accordingly. This file and c3p0-x.jar should be in the classpath.

c3p0.properties

  • c3p0.idleConnectionTestPeriod . If this number is greater than 0, c3p0 will check all inactive, merged but not dropped connections, every this number of seconds.

  • c3p0.testConnectionOnCheckout . Use only if necessary. Expensive. If true, an operation is performed each time the connection is verified to verify that the connection is valid. Best bet: periodically check connections using idleConnectionTestPeriod.

There are several other properties that can be configured in hibernate.properties and c3p0.properties.

+1
source

Maybe you are trying to call the .getCurrentSession () method instead of .openSession () ?

If the connection drops, you must install a new one.

Hope this helps.

0
source

All Articles