1

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.

Khan
  • 1,418
  • 1
  • 25
  • 49

2 Answers2

3

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" />
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • do i have to copy the c3p0 jar file in the tomcat lib folder? – Khan Jun 27 '17 at 07:44
  • okay. my hibernate config file with cp30 settings is also in my project. So this should be also on the server? – Khan Jun 27 '17 at 07:48
  • 1
    the web container (Tomcat) need not be aware of ORM. This is an application level concern. I am not familiar with hibernate, but I assume any config files will have to be placed in the classpath of each web app (probably `WebContent/WEB-INF/classes`) – Sharon Ben Asher Jun 27 '17 at 07:51
  • yes ok but i have my c3p0 seetings in my hibernate file :/. – Khan Jun 27 '17 at 07:52
  • sorry, like I said, I am using a different ORM (configure-less one) – Sharon Ben Asher Jun 27 '17 at 07:53
2

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

Yugerten
  • 878
  • 1
  • 11
  • 30
  • a little bit more explanation would be nice – Khan Jun 27 '17 at 07:59
  • 1
    in this step, i suppose that your catalina expose the JNDI (conf tomcat side done), firts conf is for hiberbate(sessionFactory) to capture the JNDI, tthe sencond conf is for bean which will use the sessionFactory. – Yugerten Jun 27 '17 at 08:04
  • not i have asked one more question today maybe you can help me https://stackoverflow.com/questions/44801615/shared-c3p0-connection-pool-with-hibernate-and-mysql-on-tomcat. I dont understand how to use sessionfactory with c3p0 on tomcat – Khan Jun 28 '17 at 13:11
  • 1
    @Khan you can see , i posted a response – Yugerten Jun 28 '17 at 13:37