Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using c3p0 with Tomcat 8 DataSource

Tags:

hibernate

c3p0

I have a tomcat 8 server with a DataSource loaded. I want to know if it is possible to use this DataSource combined with c3p0 connection pool management. So far this is what I've tried.

Context.xml

 <Context>
 ...
 <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" 
  maxIdle="30" maxTotal="100" maxWaitMillis="10000"
name="jdbc/store" password="text" type="javax.sql.DataSource" 
url="jdbc:mysql://localhost:3306/db" username="user"/>
 </Context>

hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="hibernate.connection.datasource">
        java:comp/env/jdbc/store
    </property>

    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>


    <property name="hibernate.dialect">
        org.hibernate.dialect.MySQLDialect
    </property>

...more stuff

The problem is mysql shows only one process once the server is started

like image 930
Julio Bastida Avatar asked Mar 11 '23 17:03

Julio Bastida


1 Answers

I have finally found the solution: the context.xml Resource should go like this

<Resource auth="Container" 
 name="jdbc/store" 
 type="com.mchange.v2.c3p0.ComboPooledDataSource" 
 driverClass="com.mysql.jdbc.Driver" 
 minPoolSize="5" maxPoolSize="10" 
 factory="org.apache.naming.factory.BeanFactory"
 jdbcUrl="jdbc:mysql://localhost:3306/database" 
 user="user" password="text" />

Please note that some of the common Resource attributes are different, e.g. jdbcUrl instead of url, user instead of username, etc

like image 197
Julio Bastida Avatar answered May 11 '23 02:05

Julio Bastida