title: JDBC Connection Pooling error
body: <p>I am trying to pool a <code>jdbc mysql</code> connection using <code>Tomcat</code>,in order to do this I am <a href="http://www.onjava.com/2006/04/19/database-connection-pooling-with-tomcat.html" rel="nofollow">following this blog post</a></p>
  
  <p>I have added the following code to <code>server.xml</code> in Tomcat(remoteURL and remoteDatabase represent the actual URL and database name I am passing to it):</p>
  
  <pre><code>&lt;!-- Modified as suggested by the article on jdbc connection pooling --&gt;
  &lt;Connector 
     connectionTimeout="20000" 
     port="8180" 
     protocol="HTTP/1.1" 
     redirectPort="8443"
     maxHttpHeaderSize="8192" 
     maxProcessors="150" 
     maxThreads="150" 
     minSpareThreads="25" 
     maxSpareThreads="75" 
     enableLookups="false"  
     acceptCount="150" 
     disableUploadTimeout="true"
  /&gt;   
  
   &lt;Context 
          path="/dbcp" 
          docBase="dbcp" 
          debug="5" 
          reloadable="true" 
          crossContext="true"
      &gt;
          &lt;Resource 
              name="jdbc/remoteDB" 
              auth="Container" 
              type="javax.sql.DataSource" 
              removeAbandoned="true"
              removeAbandonedTimeout="30" 
              maxActive="100"
              maxIdle="30" 
              maxWait="10000" 
              username="root"
              password="root"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://remoteURL/databaseName"/&gt;
  </code></pre>
  
  <p>I have added this to the <code>web.xml</code> in my project:</p>
  
  <pre><code>&lt;!-- Setting up DBCP connection pooling as suggested by   http://www.onjava.com/2006/04/19/database-connection-pooling-with-tomcat.html--&gt;
  &lt;listener&gt;
      &lt;listener-class&gt; com.hastha.maperp.util.DBCPoolingListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;resource-ref&gt;
      &lt;description&gt; DB Connection Pooling&lt;/description&gt;
        &lt;res-ref-name&gt; jdbc/remoteDB&lt;/res-ref-name&gt;
        &lt;res-type&gt; javax.sql.DataSource&lt;/res-type&gt;
        &lt;res-auth&gt; Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>I get <a href="http://pastebin.com/JVD6y3x0" rel="nofollow">this <code>error</code> when I try to start <code>Tomcat</code> from <code>Eclipse</code></a>.What should I set <code>docbase</code> value to get this to work?</p>
  
  <p>Also,currently I have this class that recieves the <code>DataSource</code> object from <code>ServletContextListener</code>,is there a better way of doing this:</p>
  
  <pre><code> package com.hastha.maperp.util;
  import org.apache.tomcat.jdbc.pool.DataSource;
  
  public class DataSourceFactory {
  
    public static DataSource ds;
  </code></pre>
  
  <p>}</p>
  
  <p>This is the error:</p>
  
  <pre><code>Oct 30, 2014 12:29:08 PM org.apache.catalina.core.StandardContext resourcesStart
  SEVERE: Error starting static Resources
  java.lang.IllegalArgumentException: Document base C:\Users\Vamsi\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\webapps\dbcp does not exist or is not a readable directory
      at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:136)
      at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:5089)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5269)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  
  Oct 30, 2014 12:29:08 PM org.apache.catalina.core.ContainerBase startInternal
  SEVERE: A child container failed during start
  java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/dbcp]]
      at java.util.concurrent.FutureTask.report(Unknown Source)
      at java.util.concurrent.FutureTask.get(Unknown Source)
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
      at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:799)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/dbcp]]
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
      ... 6 more
  Caused by: org.apache.catalina.LifecycleException: Error in resourceStart()
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5270)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 6 more
  
  Oct 30, 2014 12:29:09 PM org.apache.catalina.core.ContainerBase startInternal
  SEVERE: A child container failed during start
  java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
      at java.util.concurrent.FutureTask.report(Unknown Source)
      at java.util.concurrent.FutureTask.get(Unknown Source)
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
      at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:300)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:731)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
      at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
  Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  Caused by: org.apache.catalina.LifecycleException: A child container failed during start
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
      at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:799)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 6 more
  
  Oct 30, 2014 12:29:09 PM org.apache.catalina.startup.Catalina start
  SEVERE: The required Server component failed to start so Tomcat is unable to start.
  org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8105]]
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:689)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
      at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
  Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]]
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
      at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:731)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 7 more
  Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]]
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
      at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 9 more
  Caused by: org.apache.catalina.LifecycleException: A child container failed during start
      at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
      at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:300)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 11 more
  
  Oct 30, 2014 12:29:09 PM org.apache.coyote.AbstractProtocol pause
  INFO: Pausing ProtocolHandler ["http-bio-8180"]
  Oct 30, 2014 12:29:09 PM org.apache.coyote.AbstractProtocol pause
  INFO: Pausing ProtocolHandler ["ajp-bio-8109"]
  Oct 30, 2014 12:29:09 PM org.apache.catalina.core.StandardService stopInternal
  INFO: Stopping service Catalina
  Oct 30, 2014 12:29:09 PM org.apache.coyote.AbstractProtocol destroy
  INFO: Destroying ProtocolHandler ["http-bio-8180"]
  Oct 30, 2014 12:29:09 PM org.apache.coyote.AbstractProtocol destroy
  INFO: Destroying ProtocolHandler ["ajp-bio-8109"]
  Oct 30, 2014 12:29:10 PM org.apache.catalina.core.StandardContext listenerStop
  SEVERE: Exception sending context destroyed event to listener instance of class com.hastha.maperp.util.DBCPoolingListener
  java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource
      at com.hastha.maperp.util.DBCPoolingListener.contextDestroyed(DBCPoolingListener.java:23)
      at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5014)
      at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5659)
      at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
      at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1575)
      at org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1564)
      at java.util.concurrent.FutureTask.run(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  
  Oct 30, 2014 12:29:10 PM org.apache.catalina.deploy.NamingResources cleanUp
  WARNING: Failed to retrieve JNDI naming context for container [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/dbcp]] so no cleanup was performed for that container
  javax.naming.NamingException: No naming context bound to this class loader
      at org.apache.naming.ContextBindings.getClassLoader(ContextBindings.java:350)
      at org.apache.catalina.deploy.NamingResources.cleanUp(NamingResources.java:985)
      at org.apache.catalina.deploy.NamingResources.stopInternal(NamingResources.java:968)
      at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
      at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5676)
      at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
      at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:272)
      at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1041)
      at org.apache.catalina.core.ContainerBase.destroyInternal(ContainerBase.java:1234)
      at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
      at org.apache.catalina.core.ContainerBase.removeChild(ContainerBase.java:1041)
      at org.apache.catalina.core.ContainerBase.destroyInternal(ContainerBase.java:1234)
      at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
      at org.apache.catalina.core.StandardService.destroyInternal(StandardService.java:593)
      at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
      at org.apache.catalina.core.StandardServer.destroyInternal(StandardServer.java:821)
      at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:305)
      at org.apache.catalina.startup.Catalina.start(Catalina.java:693)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:321)
      at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455)
  </code></pre>