Wednesday, 20 April 2016

How to configure C3P0 Connection pool in Spring with Hibernate ?


Put the c3p0.properties file at classpath
# c3p0.properties
c3p0.maxStatementsPerConnection=100


Put jar file "c3p0-0.9.1.2.jar" at lib folder of application

Spring configuration
<bean id="dataSource1"
      class="com.mchange.v2.c3p0.ComboPooledDataSource" 
      destroy-method="close">
  <property name="driverClass">
     <value>oracle.jdbc.driver.OracleDriver</value>
  </property>
  <property name="jdbcUrl"> 
     <value>jdbc:oracle:thin:@172.16.1.216:1521:prtp</value>
  </property>
  <property name="user"><value>vodafone6</value></property>
  <property name="password"><value>vodafone6</value></property>
  <property name="initialPoolSize"><value>20</value></property>
  <property name="minPoolSize"><value>20</value></property>
  <property name="maxPoolSize"><value>100</value></property>
  <property name="maxIdleTime"><value>1800</value></property>
  <property name="idleConnectionTestPeriod">
     <value>60</value>
  </property>
  <property name="acquireIncrement"><value>1</value></property>
</bean>

<bean id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
      <ref local="dataSource1" />
  </property>
  <property name="mappingResources">
      <list>
      <value>mappings/myMapping.hbm.xml</value>
          [MORE hbm XML files... ]
      </list>
  </property>
  <property name="hibernateProperties">
      <props>
           <prop key="hibernate.dialect"> 
              org.hibernate.dialect.Oracle9Dialect 
           </prop>
           <prop key="show_sql">false</prop>
           <prop key="hibernate.c3p0.max_size">100</prop>
           <prop key="hibernate.c3p0.min_size">0</prop>
           <prop key="hibernate.c3p0.initial_pool_size">5</prop>
           <prop key="hibernate.c3p0.min_pool_size">1</prop>
           <prop key="hibernate.c3p0.max_pool_size">50</prop>
           <prop key="hibernate.c3p0.timeout">1800</prop>
           <prop key="hibernate.c3p0.max_statements">50</prop>
           <prop key="hibernate.c3p0.idleConnectionTestPeriod">
              60 
           </prop>
           <prop key="hibernate.c3p0.maxIdleTime">1800</prop>
           <prop key="hibernate.c3p0.acquire_increment">1</prop>
           <prop key="hibernate.cache.provider_class">
              net.sf.ehcache.hibernate.EhCacheProvider
           </prop>
           <prop key="hibernate.jdbc.batch_size">50</prop>
           <prop key="hibernate.hbm2ddl.auto">update</prop>   
      </props>
  </property>
</bean>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.