Search code examples
jettyjndic3p0

Close Jetty DataSources when the web application context is destroyed


I'm using the maven-jetty-plugin to run a web application in development mode. Also, I configure a c3p0 JNDI DataSource in jetty-env.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
  <Configure id="wac" class="org.mortbay.jetty.webapp.WebAppContext">
    <New id="dataSource" class="org.mortbay.jetty.plus.naming.Resource">
        <Arg>
            <Ref id="wac" />
        </Arg>
        <Arg>jdbc/MyVeryOwnDB</Arg>
        <Arg>
            <New class="com.mchange.v2.c3p0.ComboPooledDataSource">
                 <Set name="driverClass">oracle.jdbc.driver.OracleDriver</Set>
                 <Set name="jdbcUrl">jdbc:oracle:thin:@X:Y:Z</Set>
                 <Set name="user">U</Set>
                 <Set name="password">P</Set>           
            </New>
        </Arg>
    </New>
</Configure>

While this works just fine, I noticed that the connection pool is not shut down on webapp redeploy - the c3p0 threads are still alive, which means that I get both a PermGen and a JDBC connection leak.

I have found no reference to destroying defined resources in jetty-env.xml, and I'm also shy of putting in development-only hooks, since the production set up is different.

How can I make sure that when redeploying the application at development time there is no leak caused by the connection pool not being closed?


Solution

  • The answer is to remove the references from jetty-env.xml and configure an additional jettyConfig parameter using the maven-jetty-plugin.

    <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" 
    <Configure id="Server" class="org.mortbay.jetty.Server">
        <New id="dataSource" class="org.mortbay.jetty.plus.naming.Resource">
            <Arg></Arg> <!-- Global scope -->
            <!-- Other values elided for brevity -->
        </New>
    </Configure>