如何在 hibernate 中配置 c3p0 以自动刷新过时的数据库连接
我在我的应用程序中使用 hibernate 3、c3p0 9.1.2、Oracle 11g。如果我重新启动 Oracle,则过时的连接不会刷新,并且会收到异常“java.sql.SQLRecoverableException:已关闭的连接”。下面是我的 hibernate.cfg.xml。
我是 Hibernate API 的初学者。您能否建议如何配置休眠以在指定时间自动刷新过时的连接。
这是我的 hibernate.cfg.xml
oracle.jdbc.driver.OracleDriver jdbc:oracle:thin:@localhost:1521:ems 埃姆斯曼
<property name="hibernate.c3p0.idle_test_period">60</property> <!-- seconds -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="c3p0.validate">true</property>
<mapping resource="<package-name>/GroupOpWorkflow.hbm.xml"/>
<mapping resource="<package-name>/GroupOperation.hbm.xml"/>
<mapping resource="<package-name>/GroupOpNode.hbm.xml"/>
<mapping resource="<package-name>/NodeStatusLog.hbm.xml"/>
</session-factory>
I am using hibernate 3, c3p0 9.1.2, Oracle 11g in my application. If I restart the Oracle then the stale connections are not getting refresh and I am getting exception "java.sql.SQLRecoverableException: Closed Connection". Below is my hibernate.cfg.xml.
I am a beginner in Hibernate API. Can you please suggest how to configure hibernate to automatically refresh the stale connections on a specified time.
Here is my hibernate.cfg.xml
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@localhost:1521:ems
emsman
<property name="hibernate.c3p0.idle_test_period">60</property> <!-- seconds -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="c3p0.validate">true</property>
<mapping resource="<package-name>/GroupOpWorkflow.hbm.xml"/>
<mapping resource="<package-name>/GroupOperation.hbm.xml"/>
<mapping resource="<package-name>/GroupOpNode.hbm.xml"/>
<mapping resource="<package-name>/NodeStatusLog.hbm.xml"/>
</session-factory>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要配置的是
c3p0
,您的数据库连接池,而不是休眠。尝试设置idleConnectionTestPeriod
和适当的preferredTestQuery
,例如select 1 from Dual
。validate
属性已被弃用,建议您不要使用它。有关详细信息,请参阅 http://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool。如果您在
WEB-INF/classes
中创建c3p0.properties
文件,您将获得最大的控制权,但您需要确保不要覆盖中的这些属性>hibernate.cfg.xml
。It's
c3p0
, your database connection pool, that you need to configure - not hibernate. Try settingidleConnectionTestPeriod
and an appropriatepreferredTestQuery
, e.g.,select 1 from dual
. Thevalidate
property has been deprecated and it's recommended that you not use that.See http://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool for more information. You'll get the most control if you create a
c3p0.properties
file inWEB-INF/classes
but you need to make sure not to override those properties in yourhibernate.cfg.xml
.您可以尝试以下方法,这是 c3p0 的作者的简单建议,此处:
You can try the following, it's a simple advice from the author of c3p0 taken here: