为什么在服务器重启时c3p0属性上的testConnectionOnCheckout不是恢复池?

时间:2015-01-21 19:02:20

标签: java hibernate c3p0

我有一个问题,c3p0恢复服务器重新启动时的连接池 这是我的配置:hibernate.cfg.xml

<property name="hibernate.c3p0.min_size">7</property>
    <property name="hibernate.c3p0.max_size">53</property>
    <property name="hibernate.c3p0.timeout">100</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">1000</property>
    <property name="hibernate.c3p0.validate">true</property>
    <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>

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</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>

和主要课程

System.setProperty("C3P0_SYS_PROPS", "-Dcom.mchange.v2.c3p0.cfg.xml=c3p0-config.xml -Dcom.mchange.v2.log.MLog=com.mchange.v2.log.FallbackMLog -Dcom.mchange.v2.log.FallbackMLog.DE FAULT_CUTOFF_LEVEL=WARNING");
    HibernateUtilsFactory.startTransaction();
    System.out.println("restart server now! after restarted, please enter");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter String");
    dao.save(); //save operation
    HibernateUtilsFactory.commit();

那是probem,在创建会话之后,我通过等待输入来中断代码,并且当代码被中断时,我重新启动mysql服务器,重新启动后,我按回车并且代码正在恢复但是连接池没有恢复,是c3p0 -config未成功加载?或者有一些配置或代码,谢谢

1 个答案:

答案 0 :(得分:0)

看起来您希望在单个休眠会话中恢复。那不会发生。一个Connection已经被检出(嵌入在hibernate会话中),它在客户端而不是池中,然后它被无效。 c3p0将恢复,因为如果DBMS关闭然后再次恢复,当它重新启动时,新的连接将可用于新的会话,应用程序将再次运行。然而,当DBMS发生故障时,Sessions已经开始然后在中途中断,但仍然会被永久性破坏。 (对于某些DBMS,可能会有一些配置导致Connections在中断后尝试重新连接到DBMS,例如MySQL auto reconnect。)