【问题标题】:c3p0 connection pooling properties are not set?c3p0连接池属性没有设置?
【发布时间】:2012-05-19 22:12:27
【问题描述】:

我正在使用 spring/hibernate 应用程序并配置了 c3p0 连接池。 c3p0 连接池已启用。我验证了我的日志。

10 May 2012 14:55:56  INFO AbstractPoolBackedDataSource:462 - Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource 

但问题是,我以编程方式获取配置的数据源并尝试查看其属性,但是我在配置文件中设置的任何属性都没有设置。请在下面找到配置和调试值。

<prop key="hibernate.connection.pool.size">20</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.max_size">20</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.c3p0.max_statements">50</prop>
                <prop key="hibernate.c3p0.idle_test_period">3000</prop>
                <prop key="hibernate.jdbc.batch_size">50</prop>
                <prop key="hibernate.c3p0.preferredTestQuery">SELECT GETDATE()</prop>
                 <prop key="hibernate.c3p0.testConnectionOnCheckout">true</prop>

调试值:

我正在获取如下数据源。

    WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
.......getBean("datasourceId");
..................

在属性中我设置了首选测试查询。这在图像中也是空的。

我在这里有什么遗漏吗?谢谢!

【问题讨论】:

  • 你能检查一下是否在 nexstedDataSource 中设置了这些属性
  • 我确认那里也不存在。

标签: java spring hibernate c3p0


【解决方案1】:

我使用如下

<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="pooledConn"/>
    <property name="hibernateProperties">
       <props>
         <prop key="hibernate.dialect" >${HIBERNATE.DIALECT}</prop>
        <!--  <prop key="hibernate.show_sql">${HIBERNATE.SHOW_SQL}</prop> -->
          <prop key="hibernate.show_sql">true</prop>
         <prop key="hibernate.hbm2ddl.auto">${HIBERNATE.hBM2DDL.AUTO}</prop>   
       </props>
    </property> 

它对我来说绝对完美,您能否粘贴您的配置以获得更多帮助

【讨论】:

    【解决方案2】:

    尝试将以下内容添加到您的配置文件中:

    <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,花了一些时间来找出解决方案。

      我使用 Hibernate 4.0.1 和 mysql 5.1(没有 spring 框架),我遇到了这个问题。首先确保您正确配置了 c3p0 jar,这是必不可少的。

      我在 hibernate.cfg.xml 中使用了这些属性

      <property name="hibernate.c3p0.validate">true</property>
      <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
      <property name="hibernate.c3p0.min_size">5</property>
      <property name="hibernate.c3p0.max_size">20</property>
      <property name="hibernate.c3p0.max_statements">50</property>
      <property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>
      <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
      <property name="hibernate.c3p0.idle_test_period">10</property>
      <property name="hibernate.c3p0.acquireRetryAttempts">5</property>
      <property name="hibernate.c3p0.acquireRetryDelay">200</property>
      <property name="hibernate.c3p0.timeout">40</property>
      

      但这没有用,因为 C3p0 仍然采用默认属性,而不是我在 hibernate.cfg.xml 中设置的属性,您可以在日志中检查它。所以,我在很多网站上搜索了正确的解决方案,最后我想出了这个。移除 cfg.xml 中的 C3p0 属性,并在根路径中创建 c3p0-config.xml(连同 cfg.xml)并设置属性如下。

      <c3p0-config>
      <default-config> 
      <property name="automaticTestTable">con_test</property>
      <property name="checkoutTimeout">40</property> 
      <property name="idleConnectionTestPeriod">10</property> 
      <property name="initialPoolSize">10</property>
      <property name="maxPoolSize">20</property> 
      <property name="minPoolSize">5</property> 
      <property name="maxStatements">50</property>
      <property name="preferredTestQuery">SELECT 1;</property>
      <property name="acquireRetryAttempts">5</property>
      <property name="acquireRetryDelay">200</property>
      <property name="maxIdleTime">30</property>
      </default-config>
      </c3p0-config>
      

      但是如果你运行,ORM 使用 jdbc 连接而不是 C3p0 连接池,因为我们应该在 hibernate.cfg.xml 中添加这些属性

      <property name="hibernate.c3p0.validate">true</property>
      
      <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
      

      现在一切正常(至少它对我来说很好)并且问题已经解决了。

      检查以下内容以获取参考。

      http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing

      https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool

      我希望这能解决你的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-20
        • 2012-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-09
        • 1970-01-01
        相关资源
        最近更新 更多