网络故障后重新建立数据库连接 - Hibernate

3

大家好,我正在使用Hibernate ORM和Oracle数据库。我的cfg文件有以下属性:

    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@url</property>
    <property name="connection.username">username</property>
    <property name="connection.password">pasword</property>
    <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>                

    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>    
    <property name="hibernate.c3p0.acquire_increment">3</property>

一切正常,但是当我运行应用程序并且拔掉网络电缆再重新插上时,我的数据库查询失败了。它给了我一个错误提示。

java.sql.SQLException: Io exception: Connection reset by peer: socket write error

有没有重新建立连接的方法?
2个回答

2
你需要配置数据库连接池,而不是Hibernate。尝试设置idleConnectionTestPeriod和适当的preferredTestQuery,例如:select 1 from dual
更多信息请参见如何配置C3P0连接池。如果在WEB-INF/classes中创建c3p0.properties文件,您将获得最大的控制权,但必须确保不要在hibernate.cfg.xml中覆盖这些属性。
我曾经编写过类似于c3p0-config.xml的内容。
<c3p0-config>
<default-config>
<!-- Configuring Connection     Testing -->
<!-- property name="automaticTestTable">TEST_EMS_HIBERNATE_CONN</property -->
<property name="checkoutTimeout">0</property>
<property name="testConnectionOnCheckout">true</property>
<property name="testConnectionOnCheckin">false</property>
<property name="preferredTestQuery">SELECT 1 from dual</property>
<!-- Configuring Recovery From Database Outages -->
<property name="acquireRetryAttempts">0</property>
<property name="acquireRetryDelay">1000</property>
<property name="breakAfterAcquireFailure">false</property>
<!-- Configuring to     Debug and Workaround Broken     Client Apps     -->
<property name="unreturnedConnectionTimeout">1800</property>
<property name="debugUnreturnedConnectionStackTraces">true</property>
</default-config>

和系统属性一样:

C3P0_SYS_PROPS="-Dcom.mchange.v2.c3p0.cfg.xml=<FILE-PATH>/c3p0-config.xml -Dcom.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog -Dcom.mchange.v2.log.FallbackMLog.DEFAULT_CUTOFF_LEVEL=WARNING"


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接