简体   繁体   中英

MySQL Hibernate connection issue while using c3p0

I have a web application developed with struts 1.3, Hibernate3 and Spring security. The application was previously dead after 8 hours since MySQL closed the connection after the time. Then i collect the information from several posts to keep it alive for much long now it is almost 20 - 24 hour. Can any body help me out to make this thing working.

Summary

Iam unable to login after 20 hours of inactivity. Connection is closed by MySQL.

Thanks.

Please find the below snippets for the configuration of hibernate and c3p0.

Hibernate cfg file snippet

    <property name="hibernate.dialect">se.etm.ewo.hibernate.CustomDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">MY_JDBC_URL?autoReconnect=true</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.connection.autoReconnect">true</property>
    <property name="hibernate.connection.autoReconnectForPools">true</property>
    <property name="hibernate.c3p0.idle_test_period">100</property>
    <property name="hibernate.c3p0.preferredTestQuery">select 1</property>
    <property name="hibernate.c3p0.testWhileIdle">true</property>
    <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
    <property name="hibernate.c3p0.min_size">10</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>
    <property name="hibernate.current_session_context_class">thread</property>

Piece of configuration from spring

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />

    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <ref bean="hibernateProperties" />
    </property>
</bean>

<bean id="hibernateProperties"
    class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="location">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
</bean>

<!-- Data source -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="jdbcUrl">
        <value>MY_JDBC_URL?autoReconnect=true</value>
    </property>
    <property name="user">
        <value>username</value>
    </property>
    <property name="password">
        <value>password</value>
    </property>
    <property name="initialPoolSize">
        <value>10</value>
    </property>
    <property name="maxPoolSize">
        <value>30</value>
    </property>
    <property name="minPoolSize">
        <value>10</value>
    </property>
    <property name="maxConnectionAge">
        <value>3600</value>
    </property>
    <property name="maxIdleTime">
        <value>3600</value>
    </property>
    <property name="maxIdleTimeExcessConnections">
        <value>1800</value>
    </property>
    <property name="acquireRetryAttempts">
        <value>3</value>
    </property>
    <property name="acquireRetryDelay">
        <value>3000</value>
    </property>
    <property name="breakAfterAcquireFailure">
        <value>false</value>
    </property>
    <property name="preferredTestQuery">
        <value>SELECT 1</value>
    </property>
    <property name="testConnectionOnCheckout">
        <value>true</value>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="sessionFactory" />
    </property>
</bean>

C3p0 log

INFO AbstractPoolBackedDataSource:462 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 3, acquireRetryDelay -> 3000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1br9tik951qql1qj1z141xq|2a868f36, debugUnreturnedConnectionStackTraces -> true, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1br9tik951qql1qj1z141xq|2a868f36, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc:mysql://url?autoReconnect=true, lastAcquisitionFailureDefaultUser -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 3600, maxIdleTime -> 3600, maxIdleTimeExcessConnections -> 1800, maxPoolSize -> 30, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 10, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> SELECT 1, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 300, usesTraditionalReflectiveProxies -> false ]

The c3p0 log look exactly these lines , theres nothing related to issue. Can you find anything.

testWhileIdle is not a c3p0 property. It doesn't hurt anything, but may confuse you into thinkin you are doing something that you aren't. You are testing idle connections, probably much too frequently, since you are also testing Connections on checkout.

Your config is sprawled across two places. I'm unsure how the Spring and hibernate configs will interact. c3p0 DataSources dump config at INFO on pool init. You might want to verify you have the config you expect.

Re your problem, it sounds very much like you have a Connection leak. You must ensure that any Connection checked out from a DataSource is reliably close()ed in a finally method, or via try-with-resources in Java 7+.

Use c3p0 config params unreturnedConnectionTimeout and debugUnreturnedConnectionStackTraces to debug a Connection leak.

See here .

(Note: In hibernate.cfg they'd be hibernate.c3p0.unreturnedConnectionTimeout and hibernate.c3p0.debugUnreturnedConnectionStackTraces . Or you can set them more directly in your Spring xml.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM