【问题标题】:Re-establishing a db connection after a network failure - Hibernate网络故障后重新建立数据库连接 - 休眠
【发布时间】:2011-11-10 17:31:09
【问题描述】:

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

有没有办法重新建立连接?

【问题讨论】:

    标签: java hibernate jakarta-ee c3p0


    【解决方案1】:

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

    请参阅How To Configure The C3P0 ConnectionPool 了解更多信息。如果您在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.DE FAULT_CUTOFF_LEVEL=WARNING"

    【讨论】:

      【解决方案2】:

      正如我所见,您已经指定了何时测试连接,但没有指定如何测试它们。阅读它http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing。我想你应该只添加preferredTestQuery,通常它类似于SELECT 1 FROM DUAL。 也在这里阅读Something wrong with Hibernate DB connection pooler c3p0

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-29
        • 2021-03-17
        • 1970-01-01
        • 2012-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多