我经历了许多解决方案,我的问题得到了解决,但一段时间后连接超时或断开连接。2 3 天后,我得到了解决我的问题的解决方案。
许多解决方案建议使用 autoReconnect=true 但是当我浏览文档时。我在描述 autoReconnect 参数的源代码中看到了以下文本:
不推荐使用此功能,因为它有与会话状态和数据一致性相关的副作用
当我查看 Hibernate 代码时。 Hibernate的基本连接机制不支持重连,必须使用H3C0连接池(它本身并不总是支持重连)。
但是一旦使用 H3C0,默认行为似乎是处理请求,如果连接死了,那么用户会看到并出错 - 但至少它会重新连接以进行下一个请求。我想一个错误比无限错误好,但仍然不如零错误。事实证明,需要使用 testConnectionOnCheckout 选项——文档不建议使用该选项,因为在请求之前测试连接可能会导致性能下降。当然,软件首先必须工作,其次才是它必须快速工作。
因此,总而言之,要获得与“工作”的连接(我将其定义为包括通过无错误地重新连接来处理断开的连接):在“hibernate.cfg.xml”中:
<!-- hibernate.cfg.xml -->
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<!-- no "connection.pool_size" entry! -->
然后创建一个文件“c3p0.properties”,该文件必须位于类路径的根目录中(即无法为应用程序的特定部分覆盖它):
c3p0.properties
c3p0.testConnectionOnCheckout=true
如果这个解决方案不起作用,还有更多可能的解决方案:-
1. Add
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
Also dont forget to place the c3p0-0.9.1.jar in the classpath.
2. Instead of using that c3p0.properties file, couldn't you just use this property in your hibernate.cfg.xml:
<property name="hibernate.c3p0.validate">true</property>
Also checkout the last post on this page:
https://forum.hibernate.org/viewtopic.php?p=2399313
If all these not work than go [more][1] and read in detail
[1]: http://hibernatedb.blogspot.in/2009/05/automatic-reconnect-from-hibernate-to.html