idipous
idipous

Reputation: 2910

Is Hibernate c3p0 configuration needed if Tomcat JDBC datasource is configured

maybe I don't understand something correctly here. If one has configured the datasource like below

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
p:url="${db.url}" p:username="${db.username}" p:password="${db.password}" 
p:initialSize="10" 
p:minIdle="10" 
p:maxIdle="20" 
p:maxActive="50"
p:timeBetweenEvictionRunsMillis="30000"     
p:minEvictableIdleTimeMillis="60000"
p:validationQuery="SELECT 1"
p:validationInterval="30000" />

Does it make sense to add the following properties to the

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="jpaProperties">
  <props>

    <prop key="hibernate.default_schema">${jdbc.schema}</prop>
    <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>

    <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.generate_statistics">true</prop>
    <prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache.xml</prop>
    <prop key="hibernate.cache.region_prefix"></prop>
    <prop key="hibernate.cache.use_structured_entries">true</prop>

    <prop key="hibernate.c3p0.minPoolSize">5</prop>
    <prop key="hibernate.c3p0.maxPoolSize">20</prop> 
    <prop key="hibernate.c3p0.timeout">600</prop> 
    <prop key="hibernate.c3p0.max_statement">50</prop>
    <prop key="hibernate.c3p0.testConnectionOnCheckout">true</prop>

    </props>
</property>

If no. Which one is better?

In my mind, what I have configured above is the pool twice. Once with Tomcat JDBC and another with c3p0.

Upvotes: 0

Views: 1242

Answers (2)

happyyangyuan
happyyangyuan

Reputation: 179

If you want spring to get datasource from c3p0 pool, just make the LocalContainerEntityManagerFactoryBean refer the c3p0 datasource bean.Here is an example.

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    ...
</bean>
<bean id="dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" /> 

    <!-- these are C3P0 properties -->
    <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
    <property name="minPoolSize" value="${c3p0.minPoolSize}" />
    <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
    <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
</bean>

You can config the properties as you wish. You need to add c3p0 dependency into your project:

<dependency>
  <groupId>c3p0</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.1.2</version><!--Maybe this is not the newest version-->
  <type>jar</type>
  <scope>compile</scope>
</dependency>

I am wondering why don't you use JNDI to provide the data source?Tomcat as an example,in the $TOMCAT_HOME/Context.xml、Server.xml:

<Resource name="jdbc/sample" auth="Container"
    type="com.mchange.v2.c3p0.ComboPooledDataSource"
    username=...
    password=...
    url=...
    driverClassName=...
    otherAttributes...
/> 

To reference the JNDI datasource please see here. About the C3P0 pooling and spring see here and here.

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85781

The benefit that Spring gives you in this case is that you can provide your own DataSource and use the connection pool implementation you want/need regardless where the environment your app will be deployed and thus your application will be completely independent of the application server (in this case, Tomcat that works as servlet container).

From this point on, it depends on you to decide which connection pool implementation to use. IMO I would recommend using HikariCP rather than C3P0 or Tomcat. Disclaimer: I don't support HikariCP by any means, I'm just a happy user of the technology.

Upvotes: 1

Related Questions