tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicholas Sushkin <>
Subject Re: Cleaning up after tomcat jdbc-pool?
Date Thu, 03 Mar 2011 22:05:04 GMT
Hi, Filip,

My tomcat jdbc pool is configured in my app's context.xml. My app uses MyBatis
3 to do the work. I store MyBatis SqlSessionFactory in ServletContext

I was able to get DataSources back from Batis SqlSessionFactory, and invoke
.close on each. The leak seems to have gone away now.

Thanks again for your help.

ps. Here's a snippet, in case anyone runs a similar configuration

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.tomcat.jdbc.pool.DataSource;

 * Initialize MyBatis Configuration in this application
 * <p> Loads mybatis configuration for d1 and d2 Databases. Per user manual, 
 * configures one {@link SqlSessionFactory} per Database and stores the factories 
 * in the context attributes <code>smsSqlSessionFactory</code> and 
 * <code>riSqlSessionFactory</code>
 * @author nsushkin
 * @see
public class MyBatisConfiguratingContextListener implements javax.servlet.ServletContextListener

    public void contextInitialized(ServletContextEvent sce)
        ServletContext ctx = sce.getServletContext();
        String myBatisConfigFile = "…/mybatis-configuration.xml";

                    new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(myBatisConfigFile),

                    new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(myBatisConfigFile),
        catch (IOException ex)
            ctx.log("Unable to configure MyBatis from config file `" + myBatisConfigFile +

    public void contextDestroyed(ServletContextEvent sce)
        ServletContext ctx = sce.getServletContext();
        closeDataSource(ctx, "sf1");
        closeDataSource(ctx, "sf2");
    private void closeDataSource(ServletContext ctx, String contextAttribute)
        DataSource dataSource = (DataSource)((SqlSessionFactory) ctx.getAttribute(contextAttribute)).getConfiguration().getEnvironment().getDataSource();

On Thursday, March 03, 2011 11:17:43 Filip Hanik - Dev Lists wrote:
> hi Nicholas,
> where is your pool configured? In server.xml or in your application
> context? If it is configured in server.xml, then this is a bug, the thread
> should have been created with the class loader from the pool itself. If it
> is configured in the application context, then this simply means you
> forgot to call DataSource.close on the connection pool when your web
> application is stopped.
> best
> Filip

Nicholas Sushkin, Senior Software Engineer, Manager of IT Operations
Open Finance Aggregation eXchange <>

View raw message