大家好, 我正在使用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
有没有办法重新建立连接?
答案 0 :(得分:2)
您需要配置数据库连接池 - 而不是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"
答案 1 :(得分:0)
如我所见,您已指定何时进行测试连接,但尚未指定如何测试它们。阅读http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing。我想你应该添加preferredTestQuery
,通常是SELECT 1 FROM DUAL
。
另请阅读Something wrong with Hibernate DB connection pooler c3p0