如何在 hibernate 中配置 c3p0 以自动刷新过时的数据库连接

发布于 2024-11-15 03:15:12 字数 1320 浏览 2 评论 0原文

我在我的应用程序中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窗影残 2024-11-22 03:15:12

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

有关详细信息,请参阅 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 setting idleConnectionTestPeriod and an appropriate preferredTestQuery, e.g., select 1 from dual. The validate 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 in WEB-INF/classes but you need to make sure not to override those properties in your hibernate.cfg.xml.

幸福不弃 2024-11-22 03:15:12
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>
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>
故事灯 2024-11-22 03:15:12

您可以尝试以下方法,这是 c3p0 的作者简单建议此处

最好的办法通常是尝试步骤 3,看看是否有帮助
(无论你如何衡量表现),看看它是否会造成伤害(你的
应用程序受到连接中断的困扰吗?它恢复了吗
数据库重新启动得足够好?),然后决定。

第 3 步:
如果您想通过消除
从客户端代码路径进行连接测试:

将 testConnectionOnCheckout 设置为 false

将 testConnectionOnCheckin 设置为 true

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

如果数据库很少重新启动,那么快速恢复就不是问题,
考虑通过idleConnectionTestPeriod减少测试频率
比如说 300,看看客户是否因陈旧或损坏而感到困扰
连接。如果不是,请坚持使用 300,或者尝试更大的数字。
考虑将 testConnectionOnCheckin 设置回 false 以避免
办理登机手续时进行不必要的测试。或者,如果您的应用程序确实
遇到坏连接,考虑减少idleConnectionTestPeriod
并将 testConnectionOnCheckin 设置回 true。没有正确或
这些参数的值不正确:您正在权衡开销
确定测试频率的可靠性。确切的数字
并不那么关键。通常很容易找到配置
表现良好。花时间去追求“最佳”是不值得的
此处的值。

You can try the following, it's a simple advice from the author of c3p0 taken here:

The best thing to do is usually to try Step 3, see if it helps
(however you measure performance), see if it hurts (is your
application troubled by broken Connections? does it recover from
database restarts well enough?), and then decide.

Step 3:
If you'd like to improve performance by eliminating
Connection testing from clients' code path:

Set testConnectionOnCheckout to false

Set testConnectionOnCheckin to true

Set idleConnectionTestPeriod to 30, fire up you application and
observe. This is a pretty robust setting, all Connections will tested
on check-in and every 30 seconds thereafter while in the pool. Your
application should experience broken or stale Connections only very
rarely, and the pool should recover from a database shutdown and
restart quickly. But there is some overhead associated with all that
Connection testing.

If database restarts will be rare so quick recovery is not an issue,
consider reducing the frequency of tests by idleConnectionTestPeriod
to, say, 300, and see whether clients are troubled by stale or broken
Connections. If not, stick with 300, or try an even bigger number.
Consider setting testConnectionOnCheckin back to false to avoid
unnecessary tests on checkin. Alternatively, if your application does
encounter bad Connections, consider reducing idleConnectionTestPeriod
and set testConnectionOnCheckin back to true. There are no correct or
incorrect values for these parameters: you are trading off overhead
for reliability in deciding how frequently to test. The exact numbers
are not so critical. It's usually easy to find configurations that
perform well. It's rarely worth spending time in pursuit of "optimal"
values here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文