Announcement Announcement Module
Collapse
No announcement yet.
How to specify number of connections in c3po pool Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to specify number of connections in c3po pool

    I am working on a non-web based application and I am using spring with hibernate. For connection pooling I am using c3po. I want to be able to set the initial and maximum number of connections in the c3po pool. Where in my spring config do I do that. Below you will see a snippet of my config file.

    THanks

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    <property name="jdbcUrl">

    <value>jdbc:oracle:thin:@xxxx:1521:uaig</value>
    </property>
    <property name="user">
    <value>user</value>
    </property>
    <property name="password">
    <value>passwd</value>
    </property>
    </bean>

  • #2
    You have to work on the datasource definition (that's where you define the c3p0). You have to read the javadoc of com.mchange.v2.c3p0.ComboPooledDataSource to see what properties are exposed - you'll tell Spring to work on these ones. For example:
    Code:
       <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
            destroy-method="close">
            <property name="driverClass">
                <value>$&#123;db.driverClass&#125;</value>
            </property>
            <property name="jdbcUrl">
                <value>$&#123;db.url&#125;</value>
            </property>
             <!--
            <property name="user">
                <value>$&#123;db.user&#125;</value>
            </property>
            <property name="password">
                <value>$&#123;db.pass&#125;</value>
            </property>
            -->
            <property name="properties">
                <props>
                    <prop key="c3p0.acquire_increment">5</prop>
                    <prop key="c3p0.idle_test_period">100</prop>
                    <prop key="c3p0.max_size">100</prop>
                    <prop key="c3p0.max_statements">0</prop>
                    <prop key="c3p0.min_size">10</prop>
                    <prop key="user">$&#123;db.user&#125;</prop>
                    <prop key="password">$&#123;db.pass&#125;</prop>
                </props>
            </property>
        </bean>

    Comment


    • #3
      A small tip: when I configured c3p0 I remember that setting the properties directly on the class (through setter) didn't worked very well - I had to put them inside a property container like below. Maybe I'm missing something but I think the c3p0 always load the default properties from the distribution jar which overrides the params you set otherwise.

      That's why in my previous example the user and password are passed inside the property files (and the previous property tags are commented).

      Comment


      • #4
        Does not work for me

        It's been a long time since the last reply, but anyway. The example above does not work for me.

        The data in the properties tag is not recognized by c3po (except username and password). I verified that by changing values and looking at c3po's debugging output.

        This version wirked for me:

        Code:
        <bean id="dataSource" 
                  class="com.mchange.v2.c3p0.ComboPooledDataSource" 
                  destroy-method="close">
                
                <property name="driverClass"><value>${jdbc.driverClassName}</value></property>
                <property name="jdbcUrl"><value>${jdbc.url}</value></property>
                <property name="user"><value>${jdbc.username}</value></property>
                <property name="password"><value>${jdbc.password}</value></property>
                
                <property name="initialPoolSize"><value>3</value></property>
                <property name="minPoolSize"><value>3</value></property>
                <property name="maxPoolSize"><value>50</value></property>
                <!-- <property name="timeout"><value>0</value></property> -->   <!-- 0 means: no timeout -->
                <property name="idleConnectionTestPeriod"><value>200</value></property>
                <property name="acquireIncrement"><value>1</value></property>
                <property name="maxStatements"><value>0</value></property>  <!-- 0 means: statement caching is turned off.  -->
                <property name="numHelperThreads"><value>3</value></property>  <!-- 3 is default --> 
                
            </bean>

        Comment


        • #5
          If you have 0 as timeout you risk being timed out by the database which would result in bad connections.

          The database has a timeout which will time out the c3po connections from the database end but the application sees it as active and when such a dead connection is used it would result in error

          Comment

          Working...
          X