【问题标题】:How to configure c3p0 in hibernate to auto-refresh stale DB connections如何在休眠中配置 c3p0 以自动刷新陈旧的数据库连接
【发布时间】:2011-09-11 10:47:12
【问题描述】:

我在我的应用程序中使用休眠 3、c3p0 9.1.2、Oracle 11g。如果我重新启动 Oracle,那么陈旧的连接不会得到刷新,并且我会收到异常“java.sql.SQLRecoverableException: Closed Connection”。下面是我的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>

【问题讨论】:

    标签: oracle hibernate c3p0


    【解决方案1】:

    您需要配置的是 c3p0,您的数据库连接池 - 而不是休眠。尝试设置idleConnectionTestPeriod 和适当的preferredTestQuery,例如select 1 from dualvalidate 属性已被弃用,建议您不要使用它。

    请参阅http://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool 了解更多信息。如果您在WEB-INF/classes 中创建c3p0.properties 文件,您将获得最大的控制权,但您需要确保不要覆盖hibernate.cfg.xml 中的这些属性。

    【讨论】:

    • 谢谢李。我可以将 c3p0.properties 文件路径作为 java 系统属性或在应用程序启动期间加载属性吗? hibernate 会覆盖 c3p0.properties 属性吗?
    • hibernate 会覆盖我相信的 c3p0.properties。您不需要手动加载 c3p0.properties 文件,只需确保它在正确的位置可见并且它会自行配置。
    【解决方案2】:
    After gone through the document { http://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool } I found C3P0 was not a all used by hibernate. 
    
    So wrote a new C3P0 xml file and used the below system properties:
    
    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"
    
    
    So here is the final working configuration
    
    
    hibernate.cfg.xml 
    
    
     <session-factory>
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
            <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:ems</property>
    <property name="hibernate.connection.username">emsman</property>
    <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="hibernate.connection.autoReconnect">true</property>
    <property name="show_sql">false</property>
    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.c3p0.idle_test_period">300</property> <!-- In 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>
    .....
    
    
    <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-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>
    

    【讨论】:

      【解决方案3】:

      您可以尝试以下方法,这是来自 c3p0 的 作者 here简单建议

      最好的办法通常是尝试第 3 步,看看是否有帮助 (无论你如何衡量表现),看看它是否会受伤(是你的 应用程序因连接中断而烦恼?它是否从 数据库重新启动是否足够好?),然后再决定。

      第 3 步:
      如果您想通过消除 来自客户端代码路径的连接测试:

      将 testConnectionOnCheckout 设置为 false

      将 testConnectionOnCheckin 设置为 true

      将 idleConnectionTestPeriod 设置为 30,启动您的应用程序并 观察。这是一个非常强大的设置,所有连接都将测试 在登记入住时以及之后在泳池中时每 30 秒一次。您的 应用程序应该只会遇到损坏或陈旧的连接 很少,并且池应该从数据库关闭中恢复并且 快速重启。但是有一些与所有这些相关的开销 连接测试。

      如果数据库很少重启,那么快速恢复不是问题, 考虑通过 idleConnectionTestPeriod 降低测试频率 比如说300,看看客户是否被陈旧或损坏的困扰 连接。如果没有,请坚持使用 300,或者尝试更大的数字。 考虑将 testConnectionOnCheckin 设置回 false 以避免 检查时进行不必要的测试。或者,如果您的应用程序 遇到bad Connections,考虑减少idleConnectionTestPeriod 并将 testConnectionOnCheckin 设置回 true。没有正确或 这些参数的值不正确:您正在权衡开销 决定测试频率的可靠性。确切的数字 不是那么关键。通常很容易找到符合以下条件的配置 表现良好。很少值得花时间去追求“最优” 这里的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-29
        • 1970-01-01
        • 2017-09-02
        • 2023-03-29
        • 2018-06-26
        • 1970-01-01
        • 2015-08-30
        相关资源
        最近更新 更多