【问题标题】:Hibernate 4.1.9 c3p0 settings, and way too many connections to databaseHibernate 4.1.9 c3p0 设置,以及与数据库的连接太多
【发布时间】:2013-02-23 04:15:08
【问题描述】:

我在 Java Web 应用程序(使用 Oracle 11g 数据库)中使用 Hibernate 4.1.9,即使我使用 c3p0 池,我似乎也得到了一些失控的连接。

似乎这应该在我的配置文件中使用适当的属性进行管理,但我正在努力正确设置它们。

这是我的 persistence.xml 文件,其中包含属性设置:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="RPRM_PERSISTENCE_UNIT" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>

  <property name="hibernate.connection.username" value="username"/>
  <property name="hibernate.connection.password" value="********"/>        

  <property name="hibernate.connection.url" value="jdbc:oracle:thin:@xxxxx.xxxx.com:1771:xxxxxx"/>
  <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
  <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>

  <property name="hibernate.connection.provider_class" value="org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" /> <!-- hibernate 4.1.9 -->
  <property name="hibernate.c3p0.acquireIncrement" value="3"/>
  <property name="hibernate.c3p0.maxIdleTime" value="3600"/>
  <property name="hibernate.c3p0.minPoolSize" value="6"/>
  <property name="hibernate.c3p0.maxPoolSize" value="20"/>
  <property name="hibernate.c3p0.maxStatements" value="20"/>
  <property name="hibernate.c3p0.idleConnectionTestPeriod" value="1800"/> <!-- seconds -->
  <property name="hibernate.c3p0.maxConnectionAge" value="100"/>
  <property name="hibernate.c3p0.maxIdleTimeExcessConnections" value="300"/>
  <property name="hibernate.c3p0.testConnectionOnCheckin" value="true"/>
  <property name="hibernate.c3p0.preferredTestQuery" value="select 1 from dual"/>

  <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>

  <property name="hibernate.show_sql" value="false"/>
  <property name="hibernate.format_sql" value="false" />

</properties>

当我启动应用程序时,我看到 Hibernate 正在使用 c3p0 设置的信息:

Mar 7, 2013 11:15:21 AM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
Mar 7, 2013 11:15:21 AM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@330d4ac9 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@87961d4a [ 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 -> 30huhj8tjhzyr1ovdu4t|6196fc, idleConnectionTestPeriod -> 1800, initialPoolSize -> 3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 100, maxIdleTime -> 3600, maxIdleTimeExcessConnections -> 300, maxPoolSize -> 20, maxStatements -> 20, maxStatementsPerConnection -> 0, minPoolSize -> 6, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@8d40ef6e [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 30huhj8tjhzyr1ovdu4t|1e9c3f, jdbcUrl -> jdbc:oracle:thin:@xxxxx.xxxxx.com:1771:xxxxx, properties -> {user=******, password=******, autocommit=true, release_mode=auto} ], preferredTestQuery -> select 1 from dual, propertyCycle -> 0, testConnectionOnCheckin -> true, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 30huhj8tjhzyr1ovdu4t|fa0094, numHelperThreads -> 3 ]
Mar 7, 2013 11:15:24 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Mar 7, 2013 11:15:24 AM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Mar 7, 2013 11:15:24 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory

不幸的是,Oracle 大约每 2 分钟就会注册少量(通常一次 12 个)新连接(即使没有用户访问应用程序)

如果我在我的设置中遗漏了一些明显的东西,我深表歉意 - 我已经浏览了文档,并且网络试图了解所有属性,但可能遗漏了一些东西或设置了错误。

注意,我在 Tomcat 6.0.14 容器中使用休眠 4.1.9.Final 和 Oracle 11g。

感谢您的宝贵时间!

【问题讨论】:

    标签: java hibernate jdbc c3p0 persistence.xml


    【解决方案1】:

    C3P0 正在剔除超过 100 秒的连接。将您的 maxConnectionAge 设置为小于 100 秒的积极性应该可以解决您的问题。

    【讨论】:

    • @Derek 如果它关闭连接,为什么它会抛出太多连接,它应该只是创建新连接或者我错过了什么
    【解决方案2】:

    您已将 maxConnectionAge 设置为 100 秒。这意味着如果一个连接超过 100 秒,它将被强制关闭,这也意味着如果您的池处于空闲状态,它将每 100 秒创建 6 个新连接。

    文档说:

    秒,实际上是生活的时间。早于maxConnectionAge 的连接将被销毁并从池中清除。这与maxIdleTime 的不同之处在于它指的是绝对年龄。即使一个没有太多空闲的连接如果超过maxConnectionAge,也会从池中清除。零表示没有强制执行最大绝对年龄。

    将 maxConnectionAge 设置为 0 以禁用此功能或将其设置为更高的数字。

    【讨论】:

    • 谢谢你,马克!更改为 0 让我的池按我想要的方式工作......但是,@Derek 在一分钟内回复了你,可能是因为你的答案更加冗长和完整:)
    猜你喜欢
    • 2017-05-04
    • 2014-08-21
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    相关资源
    最近更新 更多