本文介绍了恢复Hibernate连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道重新建立/重试休眠连接的方法。我的意思是,例如:远程数据库已关闭,我开始申请。 Hibernate无法建立连接。它失败。但该应用程序未关闭。有没有办法让hibernate再试一次来建立connecton?



预先感谢

解决方案

您应该真正使用C3P0连接池:



有关该主题的C3P0文档中有一节:



首先,您必须正确配置c3p0,这在使用hibernate的情况下必须发生在c3p0.properties文件中。



您的c3p0.properties将这些属性重置为在数据库关闭时每3秒无限期地重新连接:

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

另外,为了避免连接池中的连接断开,请使用连接时间管理:

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

这些可能相当昂贵,但如果上述不够,则会有所帮助:

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

您可能还想检查是否存在阻止恢复的连接泄漏:

  c3p0.debugUnreturnedConnectionStackTraces = true 

最后,确保C3P0正确地挂上hibernate,为com.mchange包启用调试日志,看看C3P0是否告诉你任何关于它自己的事情。它应该陈述已加载的配置属性,看看它是否全部存在。



我希望这有助于。


Does anybody know a way to reestablish/retry again hibernate connection. I mean for example: the remote DB is down and I start my application. Hibernate is not able to establish a connection. it fails. But the application is not closed. Is there a way to say hibernate to try one more time to establish a connecton?

Thanks in advance

解决方案

You should really go for C3P0 connection pooling: http://www.mchange.com/projects/c3p0/index.html#hibernate-specific

There is a section in C3P0 documentation on that subject: http://www.mchange.com/projects/c3p0/index.html#configuring_recovery

First you have to properly configure c3p0, which in case of using hibernate must happen in c3p0.properties file.

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

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

Also, to avoid broken connections lying in your pool indefinitely, use connection age management:

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

These may be quite expensive, but helpful if above is not enough:

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

You may also want to check for connection leaks which prevent recovery:

c3p0.debugUnreturnedConnectionStackTraces = true

And finally, make sure that C3P0 is hooked with hibernate correctly, enable debug logging for "com.mchange" package and see if C3P0 tells you anything about itself. It should state configuration properties which are loaded, so see if it's all there.

I hope this helps.

这篇关于恢复Hibernate连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 16:37