java C3P0 配置!在哪里以及如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12446266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:54:50  来源:igfitidea点击:

C3P0 Configurations! Where and How?

javahibernatejpa-2.0connection-poolingc3p0

提问by Babak Behzadi

We are implementing a Web App using JPA2.0 and Hibernate3.0. Connection pool configurations are set in persistence.xml located in META-INF folder.

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



persistence.xml:

持久性.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>


We have a problem with connection pool configurations. It seems the configurations have no effect and the connection will be broken after 8 hours. Do we need another configuration file like hibernate.cfg.xml or hibernate.properties?

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

回答by Philipi Willemann

I had this same problem with the proprieties that I put in persistence.xml didn't affect c3p0.

我在persistence.xml 中放入的proprieties 也遇到了同样的问题,但不会影响c3p0。

Looking into http://www.mchange.com/projects/c3p0/index.html#configuration_filesI tried to put an xml file named c3p0-config.xmland put it in WEB-INF/classesand it work perfectly.

查看http://www.mchange.com/projects/c3p0/index.html#configuration_files我试图将一个 xml 文件命名c3p0-config.xml并放入其中WEB-INF/classes,它工作得很好。

Here is an example of a c3p0-config.xmlfile:

下面是一个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>

回答by Domenic D.

Good question, bad title. :) I think I answered this question on your re-post: Best configuration of c3p0

好问题,坏标题。:) 我想我在您的转帖中回答了这个问题:c3p0 的最佳配置

回答by The Gilbert Arenas Dagger

I had the same exact issue, my problem was that my web application container (Tomcat) was managing my database connections. I had to move the c3p0 configuration from my persistence.xml file to Tomcat's context.xml. The link Domenic D provided is a great place to start if that's your problem.

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

回答by Guest

There is a typo in your settings, it should be idle_test_periodnot idle_test_periods.

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

See this post for information about the setting: The use of c3p0.idle_test_period.

有关设置的信息,请参阅此帖子:c3p0.idle_test_period 的使用。