Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to shutdown connection pool on context unload?
    primarykey
    data
    text
    <p>after developing several webapps that all had a similar setup with spring, hibernate and c3p0 as connectionpool I wanted to investigate a problem that i noticed everytime: Connectionpool keeps the connections until you shutdown tomcat (or your application server).</p> <p>Today i create the most basic project I could with these four dependencies:</p> <pre><code>org.springframework:spring-web org.springframework:spring-orm org.hibernate:hibernate-core c3p0:c3p0 </code></pre> <p>(plus the specific JDBC driver).</p> <p>My web.xml only creates a ContextLoaderListener that sets up the application context.</p> <pre><code>&lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/applicationContext.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; </code></pre> <p>application context consists of two beans - datasource and session factory:</p> <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&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:postgresql://localhost/mydb&lt;/value&gt; &lt;/property&gt; &lt;property name="user"&gt; &lt;value&gt;usr&lt;/value&gt; &lt;/property&gt; &lt;property name="password"&gt; &lt;value&gt;pwd&lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>When i start the webapp and either look into jconsole's MBeans or check my DBMS for open connections i notice the three initial connections made by c3p0.</p> <p>PROBLEM: When i tell tomcat to stop the webapp, they still remain!</p> <p>I created another ServletContextListener that only has the contextDestroyed method implemented and programatically shuts down the sessionFactory (and also deregisters the JDBC drivers which is also not done automatically). Code is:</p> <pre><code>@Override public void contextDestroyed(ServletContextEvent sce) { ... sessionFactory().close(); // deregister sql driver(s) Enumeration&lt;Driver&gt; drivers = DriverManager.getDrivers(); while (drivers.hasMoreElements()) { Driver driver = drivers.nextElement(); try { DriverManager.deregisterDriver(driver); log.info("deregistering jdbc driver: " + driver); } catch (SQLException e) { log.error("error deregistering jdbc driver: " + driver, e); } } } </code></pre> <p>But is that really it? Is there no built-in mechanism that I am not aware of?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload