Khan
Khan

Reputation: 1518

Is c3p0 configurable on the server instead of WAR file?

Iam learning how to configure hibernate with tomcat. Now i want to configure connection pooling on tomcat.

I want to configure connection pooling on the server to share the pool with two applications. At the moment i have a java app including all my jar files. -Mysql-connector- -hibernate- -c3p0-

Tomcat has a build in pool that connection can be obtained with JNDI. Now how can i use c3p0 to share my pool with multiple apps?

Do i have to use the build in pool? i dont want to deploy multple pools with my WAR files.

Upvotes: 1

Views: 214

Answers (2)

Yugerten
Yugerten

Reputation: 898

you can use this config in your apps (~.cfg.xml) to check the DataSource by JNDI

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate 
 Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
    <property name="dataSource">java:comp/env/jdbc/JNDI_Name</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- your dialect-->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <mapping resource="mapping/xxx.hbm.xml"/>
    <mapping resource="mapping/yyy.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

If you use Spring xml conf, create an xml file for Datasource && sessionFactory && transactionManager:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd">


<tx:annotation-driven />
<context:component-scan base-package="x.y.z.*" />
<context:annotation-config />

<import resource="classpath:~/~.cfg.xml" />

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/Your_JNDI_Name"/>
</bean>

<!--  OR ORM HIBERNATE PART -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="configLocation">
        <value>classpath:~/~.cfg.xml</value>
    </property>
    <property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
      p:sessionFactory-ref="sessionFactory">
</bean>

<!-- Declaration of DOA beans Hibernate -->
<bean id="myDao" class="class_dao">
    <property name="sessionFactory">
        <ref bean="sessionFactory"/>
    </property>
</bean>

I hope that's helpful for you

Upvotes: 2

Sharon Ben Asher
Sharon Ben Asher

Reputation: 14328

You can use whatever connection pooling you wish with Tomcat and JNDI. You specify the pool with the type property of the Resource tag. If you want this to be accessible to multiple web apps, then the resource needs to be specified in server.xml

For example

<Resource name="jdbc/myDataSource" 
  auth="Container"
  factory="org.apache.naming.factory.BeanFactory"
  type="com.mchange.v2.c3p0.ComboPooledDataSource" 
  driverClassName="com.mysql.jdbc.Driver"
  jdbcUrl="jdbc:mysql://localhost/myDataSource" 
  user="user" password="password"
  minPoolSize="3" 
  maxPoolSize="15" 
  maxIdleTime="5000"
  idleConnectionTestPeriod="300" 
  acquireIncrement="3" />

Upvotes: 3

Related Questions