user720618
user720618

Reputation:

Re-establishing a db connection after a network failure - Hibernate

Hello everyone, I am using hibernate ORM and oracle database. My cfg file has following properties:

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

Everything works fine, but when I run the application and if I unplug network cable and plug it agian my db queries fail. It gives me the error

java.sql.SQLException: Io exception: Connection reset by peer: socket write error

Is there any way to re establish the connection?

Upvotes: 3

Views: 6563

Answers (2)

bilash.saha
bilash.saha

Reputation: 7296

You need to configure your database connection pool - not hibernate.Try setting idleConnectionTestPeriod and an appropriate preferredTestQuery, e.g., select 1 from dual.

See How To Configure The C3P0 ConnectionPool 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.

Well I had written c3p0-config.xml like

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

and the system properties like:

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"

Upvotes: 2

kan
kan

Reputation: 28951

As I see, you have specified when test connection, but have not specified how to test them. Read it http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing . I guess you should just add preferredTestQuery, usually it's something like SELECT 1 FROM DUAL. Also read here Something wrong with Hibernate DB connection pooler c3p0

Upvotes: 0

Related Questions