Search code examples
javasql-serverhibernatejpac3p0

c3p0 do not timeout when are impossible to reach the host


The first thing my program do when open is try to connect in the database, if the c3p0 wont connect it freezes and do not raise any error message.

The weird thing is that with MySql I got java.net.UnknownHostException, but with SqlServer nothing, it keeps trying forever.

I want to catch the error and do something. I tryed set this properties but no luck.

<property name="hibernate.c3p0.timeout" value="60"/>
<property name="hibernate.c3p0.unreturnedConnectionTimeout" value="60"/>

My persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.lala.Something</class>
        <shared-cache-mode>NONE</shared-cache-mode>
        <properties>
            <!-- connection -->
            <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost\\\\SQLEXPRESS:1433;databaseName=str"/>
            <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
            <property name="javax.persistence.jdbc.user" value="teste"/>
            <property name="javax.persistence.jdbc.password" value="teste"/>
            <!-- hibernate -->
            <property name="hibernate.format_sql" value="false"/>
            <property name="hibernate.show_sql" value="false"/>
            <property name="hibernate.use_sql_comments" value="false"/>
            <!-- c3p0 -->
            <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
            <property name="hibernate.c3p0.min_size" value="1"/>
        </properties>
    </persistence-unit>
</persistence>

Solution

  • I resolved adding this two properties:

    acquireRetryAttempts

    Defines how many times c3p0 will try to acquire a new Connection from the database before giving up. If this value is less than or equal to zero, c3p0 will keep trying to fetch a Connection indefinitely.

    breakAfterAcquireFailure

    If true, a pooled DataSource will declare itself broken and be permanently closed if a Connection cannot be obtained from the database after making acquireRetryAttempts to acquire one. If false, failure to obtain a Connection will cause all Threads waiting for the pool to acquire a Connection to throw an Exception, but the DataSource will remain valid, and will attempt to acquire again following a call to getConnection().

            <property name="hibernate.c3p0.acquireRetryAttempts" value="3"/>
            <property name="hibernate.c3p0.breakAfterAcquireFailure" value="true"/>