java 恢复休眠连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4726512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recover Hibernate connection
提问by Andrey
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?
有没有人知道重新建立/重试休眠连接的方法。我的意思是例如:远程数据库关闭,我启动了我的应用程序。Hibernate 无法建立连接。它失败。但该应用程序并未关闭。有没有办法说休眠再试一次建立连接?
Thanks in advance
提前致谢
回答by Spajus
You should really go for C3P0 connection pooling: http://www.mchange.com/projects/c3p0/index.html#hibernate-specific
你真的应该去 C3P0 连接池: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
C3P0 文档中有一个关于该主题的部分: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.
首先,您必须正确配置 c3p0,在使用 hibernate 的情况下,必须在 c3p0.properties 文件中进行配置。
In your c3p0.properties put these properties to retry to reconnect indefinitely every 3 seconds when database is down:
在您的 c3p0.properties 中,当数据库关闭时,每 3 秒将这些属性无限期地重试重新连接:
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.
最后,确保 C3P0 与 hibernate 正确挂钩,为“com.mchange”包启用调试日志记录并查看 C3P0 是否告诉您有关其自身的任何信息。它应该说明加载的配置属性,所以看看它是否都在那里。
I hope this helps.
我希望这有帮助。
回答by Nayan Wadekar
C3P0 is the internal connection-pool implementation for hibernate.
C3P0 是休眠的内部连接池实现。
Add "hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider" in hibernate properties file. Create a file c3p0.properties setting the parameters accordingly. This file & c3p0-x.jar must be in classpath.
在休眠属性文件中添加“hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider”。创建一个文件 c3p0.properties 相应地设置参数。该文件和 c3p0-x.jar 必须在类路径中。
c3p0.properties
c3p0.properties
c3p0.idleConnectionTestPeriod: If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds.
c3p0.testConnectionOnCheckout: Use only if necessary. Expensive. If true, an operation will be performed at every connection checkout to verify that the connection is valid. Better choice: verify connections periodically using idleConnectionTestPeriod.
c3p0.idleConnectionTestPeriod:如果这是一个大于 0 的数字,c3p0 将每隔此秒数测试所有空闲、汇集但未签出的连接。
c3p0.testConnectionOnCheckout:仅在必要时使用。昂贵的。如果为 true,将在每次连接检出时执行操作以验证连接是否有效。更好的选择:使用 idleConnectionTestPeriod 定期验证连接。
There are several other properties that can be configured in hibernate.properties & c3p0.properties.
还有其他几个属性可以在 hibernate.properties & c3p0.properties 中配置。
回答by Oleg Poltoratskii
May be, you try to call method .getCurrentSession()instead .openSession()?
可能是,您尝试调用方法.getCurrentSession()而不是.openSession()?
If connection falls you must establish new one.
如果连接断开,您必须建立新的连接。
I hope this helps.
我希望这有帮助。