gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:44:03 26 4
gpt4 key购买 nike

大家好, 我正在使用 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

有什么方法可以重新建立连接吗?

最佳答案

您需要配置您的数据库连接池 - 而不是 hibernate 。尝试设置 idleConnectionTestPeriod和适当的 preferredTestQuery ,例如 select 1 from dual .

参见 How To Configure The C3P0 ConnectionPool了解更多信息。如果您创建 c3p0.properties,您将获得最大的控制权文件在 WEB-INF/classes但你需要确保不要覆盖你的 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.DE
FAULT_CUTOFF_LEVEL=WARNING"

关于java - 网络故障后重新建立数据库连接 - Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8083759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com