在达到hibernate.c3p0.timeout后,数据库连接会发生什么?

时间:2016-09-04 20:41:25

标签: java mysql spring hibernate tomcat

在docs中,我读过下一篇文章:

  

秒可以保持连接,但在使用之前未使用   丢弃。零表示空闲连接永不过期。

所以,如果我理解正确,在连接到达hibernate.c3p0.timeout之后,必须将其从池中删除,并且还必须关闭与数据库的物理连接?或不?

我通过My​​SQL Workbench客户端连接监视所有连接,并看到创建的连接永不过期。此外,创建的连接数量大于hibernate.c3p0.max_size参数设置的数量。此外,在一段时间后,连接会在客户端连接窗口中将其时间值重置为0。这意味着他们被检查\用于他们实际上当时没有使用的情况。为什么会这样?无论如何,它适用于hibernate.c3p0.min_size参数,因为每次都会创建适当数量的连接。我也在Tomcat日志中看到了正确的配置值。我尝试了hibernate.connection.release_mode,但它没有给出任何结果。

我使用SpringORM的HibernateTemplate,我有下一个配置:

hibernate.cfg.xml中

<hibernate-configuration>
    <session-factory>

    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.use_sql_comments">true</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.generate_statistics">true</property>

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.datasource">jdbc:mysql://localhost/easywordweb</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/easywordweb</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>

    <!--<property name="hibernate.connection.pool_size">140</property>-->
    <!--<property name="hibernate.c3p0.max_size">140</property>--> 
    <property name="hibernate.c3p0.max_size">9</property> 
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.acquire_increment">3</property>
    <!--max to cache-->
    <property name="hibernate.c3p0.max_statements">50</property>

    <!--The seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire. Hibernate default: 0-->
    <property name="hibernate.c3p0.timeout">40</property>

    <!--<property name="hibernate.c3p0.validate">true</property>--> 
    <!--<property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>-->
    <!--for test, change futher-->
    <!----> 
    <!--at every connection checkin to verify that the connection is valid-->
    <!--<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>-->
    <!--at every connection checkout to verify that the connection is valid-->
    <!--<property name="hibernate.c3p0.testConnectionOnCheckin">true</property>-->
    <!--/for test, change futher-->
    <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>


    <property name="hibernate.jdbc.batch_size">20</property>
    <!--Number rows to be returned if no setted-->
    <property name="hibernate.jdbc.fetch_size">20</property>
    <property name="hibernate.jdbc.use_get_generated_keys">true</property>

    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>

    <!--FIXING: Table "...".hibernate_sequence table not found.-->
    <property name="hibernate.id.new_generator_mappings">false</property>
    </session-factory>
</hibernate-configuration>

Tomcat 使用下一个配置运行:

com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getPoolManager
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@11c5be52
[ 
    connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@87645e0f 
    [ 
        acquireIncrement -> 3, 
        acquireRetryAttempts -> 30, 
        acquireRetryDelay -> 1000, 
        autoCommitOnClose -> false, 
        automaticTestTable -> null, 
        breakAfterAcquireFailure -> false,
        checkoutTimeout -> 0, 
        connectionCustomizerClassName -> null,
        connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester,
        debugUnreturnedConnectionStackTraces -> false, 
        factoryClassLocation -> null, 
        forceIgnoreUnresolvedTransactions -> false,
        identityToken -> valididentitytokenhere, 
        idleConnectionTestPeriod -> 0, 
        initialPoolSize -> 5, 
        maxAdministrativeTaskTime -> 0, 
        maxConnectionAge -> 0, 
        maxIdleTime -> 40,
        maxIdleTimeExcessConnections -> 0, 
        maxPoolSize -> 9, 
        maxStatements -> 50, 
        maxStatementsPerConnection -> 0, 
        minPoolSize -> 5, 
        nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@82d61130 
        [ 
            description -> null, 
            driverClass -> null, 
            factoryClassLocation -> null, 
            identityToken -> valididentitytokenhere, 
            jdbcUrl -> jdbc:mysql://localhost/easywordweb, 
            properties -> { user=******, password=******} 
        ], 
        preferredTestQuery -> null, 
        propertyCycle -> 0, 
        statementCacheNumDeferredCloseThreads -> 0, 
        testConnectionOnCheckin -> false, 
        testConnectionOnCheckout -> false, 
        unreturnedConnectionTimeout -> 0, 
        usesTraditionalReflectiveProxies -> false; 
        userOverrides: {} 
    ], 
    dataSourceName -> null, 
    factoryClassLocation -> null, 
    identityToken -> valididentitytokenhere, 
    numHelperThreads -> 3 
]

弹簧context.xml中

<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"
    xmlns:mvc="http://www.springframework.org/schema/mvc">

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="packagesToScan" value="milkiv.easyword.models"/>
    <property name="configLocations">
        <value>classpath:resources/hibernate.cfg.xml</value>
    </property>
    </bean>

    <bean class="org.springframework.orm.hibernate5.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>



    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean class="milkiv.easyword.service.JsonConverter"></bean>
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> 

    <mvc:resources mapping="/resources/**" location="/resources/"/>
    <context:annotation-config/>
    <context:component-scan base-package="milkiv.easyword"/>
    <tx:annotation-driven/>
    <mvc:annotation-driven>
    <mvc:argument-resolvers>
        <bean class="org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver" />
    </mvc:argument-resolvers>
    </mvc:annotation-driven>
</beans>

我将不胜感激任何解释,链接和帮助。 提前谢谢大家!

1 个答案:

答案 0 :(得分:1)

让我们考虑一下。最初创建最小连接数。如果没有足够的连接打开,直到达到最大数量。

现在,如果应用程序不需要这么多连接,则其中一些连接不会被使用 - 空闲连接。如果连接闲置40秒(在您的情况下),它将关闭,直到达到最小量。如果不时使用连接(您的应用程序使用连接),例如每30秒连接永远不会关闭。