简体   繁体   中英

c3p0 connection pooling properties are not set?

I am using spring/hibernate application and configured c3p0 connection pooling. c3p0 connection pooling is enabled. i verified my logs.

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

But the problem is, programmatically i am getting configured datasource and tried to see its properties but what ever properties i set in configuration file, they are not set. Please find below configuration and debugged values.

<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>

debugged values:

在此处输入图片说明

i am getting datasource as below.

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

in properties i set preferred test query. that is also null in the image.

am i missing any thing here. Thanks!

I use it as follows

<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> 

and it works absolutely perfect for me , could you please paste your config for more help

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

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

I was getting the same problem and it took time to figure out the solution.

I use Hibernate 4.0.1 and mysql 5.1(no spring framework) and I was facing the issue. First make sure that you configured the c3p0 jars properly which are essential.

I used these properties in 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>

But it's of no use 'cause C3p0 was still taking the default properties not the properties which I set in hibernate.cfg.xml, You can check it in logs. So, I searched many websites for right solution and finally I came up with this. remove the C3p0 properties in cfg.xml and create c3p0-config.xml in the root path(along with cfg.xml) and set properties as follows.

<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>

but if you run, ORM takes the jdbc connection but not C3p0 connection pool 'cause we should add these properties in hibernate.cfg.xml

<property name="hibernate.c3p0.validate">true</property>

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

now everything works fine(At least it worked fine for me) and the issue is solved.

check the following for references.

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

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

I hope this solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM