title: Where to put c3p0 dependency in Tomcat Container
body: <p>I was under the impression that the libraries for both the database driver (<code>postgres-x.x.jar</code>'in my case) and the connection pooler (<code>c3p0</code>) had to reside in the container's lib (e.g. for Tomcat7, <code>$CATALINA_HOME/lib</code>).</p>
  
  <p>However, the official C3p0 documentation <a href="http://www.mchange.com/projects/c3p0/#installation" rel="nofollow noreferrer">doesn't provide any information</a> with regards to put the connection pool's jar in the container vs having it in the application's war:</p>
  
  <blockquote>
    <p>Place the files lib/c3p0-0.9.5.2.jar and lib/mchange-commons-java-0.2.11.jar somewhere in your CLASSPATH (or any other place where your application's classloader will find it). That's it! </p>
  </blockquote>
  
  <p>A current issue in a new tomcat installation (j<code>ava.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector</code>, when I have the m<code>change-commons-java dependency</code> in the application's WAR and the <code>c3p0</code> dependency in <code>$CATALINA_HOME/lib</code>) made me revisit this, but I can't find any authoritative information regarding where to put these libraries.</p>
  
  <p><strong>Usual application configuration</strong><br>
  In my case the <code>c3p0</code> configuration is made via spring bean within the application's classpath:</p>
  
  <pre><code>&lt;bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          scope="singleton" destroy-method="close"&gt;
          &lt;property name="driverClass"&gt;
              &lt;value&gt;org.postgresql.Driver&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="jdbcUrl"&gt;
              &lt;value&gt;${jdbc.url}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="user"&gt;
              &lt;value&gt;${jdbc.user}&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="password"&gt;
              &lt;value&gt;${jdbc.pw}&lt;/value&gt;
          &lt;/property&gt;
          ...
  &lt;bean&gt; 
  </code></pre>
  
  <p>If I have multiple applications in the same <code>Tomcat</code> container, each will have one c3p0 bean like this contained in its war. </p>
  
  <p><strong>Memory Leaks?</strong></p>
  
  <p>The assumption for having the <code>postgres.jar</code> and <code>c3p0.jar</code> in the container's <code>lib/</code> and not in the war was that the latter would cause memory leaks.</p>
  
  <p><strong>Leak in Postgres Driver</strong></p>
  
  <p><a href="https://stackoverflow.com/a/7198049/475829">This user states that</a> <code>JDBC drivers register themselves in the JVM-wide singleton DriverManager which is shared by all web apps. If you have the same (as in class name) JDBC driver register twice from two different web apps, this might cause your problem. This is even more problematic if your web apps use different versions of the same JDBC driver.</code></p>
  
  <p><strong>Leak in c3p0</strong>
  We moved c3p0 to <code>$CATALINA_HOME/lib</code> after <a href="https://stackoverflow.com/a/4117215/475829">this comment on Stackoverflow</a> (we had a similar warning when undeploying an application).</p>
  
  <p>Should they reside in Tomcat or the application's lib/ ? </p>