Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

c3p0, Tomcat and ComboPooledDataSource

843859Dec 3 2008 — edited Nov 20 2014
I am trying to create a c3p0 connection pool in Tomcat.

In my context.xml, I have the following:
        <GlobalNamingResources>
     
            <Resource name="jdbc/test"
                      auth="Container"
                      type="com.mchange.v2.c3p0.ComboPooledDataSource"/>
     
            <ResourceParams name="jdbc/test">
                <parameter>
                    <name>description</name>
                    <value>Test Database Connection</value>
                </parameter>
                <parameter>
                    <name>driverClassName</name>
                    <value>com.mysql.jdbc.Driver</value>
                </parameter>
                <parameter>
                    <name>user</name>
                    <value>root</value>
                </parameter>
                <parameter>
                    <name>password</name>
                    <value>******</value>
                </parameter>
                <parameter>
                    <name>jdbcUrl</name>
                    <value>jdbc:mysql://localhost:3306/test?autoReconnect=true</value>
                    </parameter>
                <parameter>
                    <name>maxPoolSize</name>
                    <value>25</value>
                </parameter>
                <parameter>
                    <name>minPoolSize</name>
                    <value>5</value>
                </parameter>
                <parameter>
                    <name>factory</name>
                    <value>com.mchange.v2.c3p0.DataSources</value>
                </parameter>
            </ResourceParams>
        </GlobalNamingResources>
In my web.xml, I have the following:
    	<resource-ref>
        	<description>Test Database Connection</description>
        	<res-ref-name>jdbc/test</res-ref-name>
        	<res-type>javax.sql.DataSource</res-type>
        	<res-auth>Container</res-auth>
    	</resource-ref>
And in my class, I am accessing using the following:
    	public static void ping() {
    		try {
    			if(context == null)
    				context = new InitialContext();
    			
    			PooledDataSource dataSource = (PooledDataSource)context.lookup("java:/comp/env/jdbc/test");
    			
    			System.out.println("Number of total connections: " + dataSource.getNumConnectionsDefaultUser());
    			System.out.println("Number of used connections: " + dataSource.getNumBusyConnectionsDefaultUser());
    		} catch(NamingException e) {
    			e.printStackTrace();
    		} catch(SQLException e) {
    			e.printStackTrace();
    		}
    	}
I am getting the following exception:
    java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to com.mchange.v2.c3p0.PooledDataSource
    	at com.ConnectionPool.ping(ConnectionPool.java:41)
    	at org.apache.jsp.jsp.test.ConnectionPool_jsp._jspService(ConnectionPool_jsp.java:57)
    	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
    	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
    	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    	at java.lang.Thread.run(Thread.java:619)
My question is, why is Tomcat casting this to an instance of org.apache.tomcat.dbcp.dbcp.BasicDataSource (why isn't it staying as a ComboPooledDataSource) and what can I do to get it as a PooledDataSource object so that I can call the getNumConnections() method on it?

Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2008
Added on Dec 3 2008
0 comments
625 views