【问题标题】:C3P0 Configurations! Where and How?C3P0 配置!在哪里以及如何?
【发布时间】:2012-09-16 10:44:31
【问题描述】:

我们正在使用 JPA2.0 和 Hibernate3.0 实现一个 Web 应用程序。连接池配置在 META-INF 文件夹中的 persistence.xml 中设置。


persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
    <persistence-unit name="MyPU" transaction-type="RESOURCE_LOCAL">
        <!-- Entity Classes-->
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="bytecode.provider"   value="org.hibernate.bytecode.javassist.BytecodeProviderImpl"/>
            <property name="hibernate.connection.username" value="{username}"/>
            <property name="hibernate.connection.password" value="{password}"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.connection.url" value="{jdbc url}"/>

            <property name="hibernate.c3p0.min_size" value="1"/>
            <property name="hibernate.c3p0.timeout" value="1000"/>
            <property name="hibernate.c3p0.acquire_increment" value="1"/>
            <property name="hibernate.c3p0.idle_test_periods" value="600"/>
            <property name="hibernate.c3p0.testConnectionOnCheckin" value="true"/>
            <property name="hibernate.c3p0.preferredTestQuery" value="SELECT 1;"/>
       </properties>
    </persistence-unit>
</persistence>

我们的连接池配置有问题。配置好像没有效果,8小时后连接断开。 我们是否需要另一个配置文件,例如 hibernate.cfg.xml 或 hibernate.properties?

【问题讨论】:

标签: java hibernate jpa-2.0 connection-pooling c3p0


【解决方案1】:

好问题,坏标题。 :) 我想我在你的转贴中回答了这个问题:Best configuration of c3p0

【讨论】:

    【解决方案2】:

    你的设置有错字,应该是idle_test_period而不是idle_test_periods

    有关设置的信息,请参阅此帖子:The use of c3p0.idle_test_period.

    【讨论】:

      【解决方案3】:

      我在persistence.xml 中放入的属性也有同样的问题,但并未影响c3p0。

      查看http://www.mchange.com/projects/c3p0/index.html#configuration_files 我尝试将一个名为c3p0-config.xml 的xml 文件放入WEB-INF/classes 中,它可以完美运行。

      这是c3p0-config.xml 文件的示例:

      <c3p0-config>
        <default-config>
          <property name="automaticTestTable">con_test</property>
          <property name="checkoutTimeout">30000</property>
          <property name="idleConnectionTestPeriod">30</property>
          <property name="initialPoolSize">10</property>
          <property name="maxIdleTime">30</property>
          <property name="maxPoolSize">100</property>
          <property name="minPoolSize">10</property>
          <property name="maxStatements">200</property>
      
          <user-overrides user="test-user">
            <property name="maxPoolSize">10</property>
            <property name="minPoolSize">1</property>
            <property name="maxStatements">0</property>
          </user-overrides>
      
        </default-config>
      </c3p0-config>
      

      【讨论】:

      • Philipi Willemann 是对的,如果您添加 c3p0 config xml,属性将被正确读取。注:如果添加一些已经在 hibernate.config 文件中描述的配置属性,hibernate 将忽略它们。作为参考,您可以在“通过 c3p0-config.xml 覆盖 c3p0 默认值”部分下的 mchange.com/projects/c3p0/index.html#configuration_files 找到更多信息
      【解决方案4】:

      我遇到了同样的问题,我的问题是我的 Web 应用程序容器 (Tomcat) 正在管理我的数据库连接。我不得不将 c3p0 配置从我的 persistence.xml 文件移动到 Tomcat 的 context.xml。如果这是您的问题,Domenic D 提供的链接是一个很好的起点。

      【讨论】:

        猜你喜欢
        • 2011-09-04
        • 2017-10-05
        • 2014-12-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-21
        • 2013-01-29
        • 2011-11-08
        • 1970-01-01
        相关资源
        最近更新 更多