Julio Bastida
Julio Bastida

Reputation: 1219

Using c3p0 with Tomcat 8 DataSource

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

Upvotes: 4

Views: 4420

Answers (1)

Julio Bastida
Julio Bastida

Reputation: 1219

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

Upvotes: 7

Related Questions