使用c3p0时MySQL Hibernate连接问题

时间:2014-11-11 11:58:47

标签: java mysql hibernate c3p0

我有一个使用struts 1.3,Hibernate3和Spring安全性开发的Web应用程序。自从MySQL关闭连接后8小时后,该应用程序已经死了。然后我从几个帖子中收集信息以保持它很长时间,现在它几乎是20-24小时。任何人都可以帮我解决这个问题。

摘要

Iam无法在20小时不活动后登录。连接由MySQL关闭。

感谢。

请查看以下有关hibernate和c3p0配置的摘录。

Hibernate cfg文件摘要

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

来自春天的配置

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

  

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

c3p0日志看起来正是这些行,与问题无关。你能找到什么吗?

1 个答案:

答案 0 :(得分:2)

testWhileIdle不是c3p0属性。它不会伤害任何东西,但可能会让你感到困惑,因为你正在做一些你不做的事情。您正在测试空闲连接,可能过于频繁,因为您还在结帐时测试连接。

您的配置遍布两个地方。我不确定Spring和hibernate配置是如何交互的。 c3p0 DataSources在池初始化的INFO上转储配置。您可能想要验证您是否具有预期的配置。

重新解决你的问题,听起来很像你有连接泄漏。您必须确保从DataSource检出的任何Connection在finally方法中可靠地关闭(),或通过Java 7 +中的try-with-resources确保。

使用c3p0 config params unreturnedConnectionTimeoutdebugUnreturnedConnectionStackTraces来调试连接泄漏。

请参阅here

(注意:在hibernate.cfg中,它们是hibernate.c3p0.unreturnedConnectionTimeouthibernate.c3p0.debugUnreturnedConnectionStackTraces。或者你可以在Spring xml中更直接地设置它们。)