【发布时间】:2017-01-12 06:38:34
【问题描述】:
在文档中,我已经阅读了下一篇:
连接可以在被连接之前保持池化但未使用的秒数 丢弃。零表示空闲连接永不过期。
所以,如果我理解正确,在连接达到hibernate.c3p0.timeout 后,它必须从池中删除,并且与数据库的物理连接也必须关闭?或不?
我正在监视 MySQL Workbench Client Connections 的所有连接,并看到创建的连接永不过期。此外,创建的连接数量大于我通过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
]
spring-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>
我将不胜感激任何解释、链接和帮助。 提前谢谢大家!
【问题讨论】:
标签: java mysql spring hibernate tomcat