Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:04:17+00:00 2026-05-28T05:04:17+00:00

I am trying to implement jdbc-pool in a standalone web app (self contained –

  • 0

I am trying to implement jdbc-pool in a standalone web app (self contained – not relying on server.xml) so that it can be moved to tomcat installations that may be earlier than 7.0.

I am connecting to MSSQL Server with the sourceforge driver (net.sourceforge.jtds.jdbc.Driver)

Everything runs fine except for this error:

SEVERE: The web application [/jdbc-pool] appears to have started a
thread named [[Pool-Cleaner]:Tomcat Connection Pool[1-12524859]] but
has failed to stop it. This is very likely to create a memory leak.

Based on this I determined that I need to close the jdbc-pool datasource. I am having trouble with this last line from that post though:

>> 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.

> This is confusing advice because javax.sql.DataSource doesn’t have
a close() method.

In order to call close, one has to cast it to what ever the data
source you are using.

How do I find out what type of datasource I am using and where is the class for it? Can I extract it from the driver jar somehow?

In addition to a servlet which uses the pool, I am using a ServletContextListener so that I can start out with pooled connections immediately from the contextInitialized method. I started adding the code to kill the connection in the contextDestroyed method of this ServletContextListener but got hung up where the question marks are:

import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;

public class JdbcPoolListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent myServletContextEvent) {

        // initialize jdbc-pool datasource to start out with pooled connections 
        try {
            Context myContext = (Context) new InitialContext().lookup("java:comp/env");
            DataSource myDataSource = (DataSource) myContext.lookup("jdbc/db");
            myServletContextEvent.getServletContext().setAttribute("JdbcPool", myDataSource);
        } catch (NamingException e) {
            System.out.println("Error initializing jdbc-pool datasource");
            e.printStackTrace();
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent myServletContextEvent) {

        // failed attempt to close the data source
        ServletContext myServletContext = myServletContextEvent.getServletContext();
        //DataSource myDataSource = (DataSource) myServletContext.getAttribute("JdbcPool");
        DataSource dataSource = (DataSource)((???) myServletContext.getAttribute(contextAttribute)).getConfiguration().getEnvironment().getDataSource();
        dataSource.close();
        myServletContext.removeAttribute("JdbcPool");

        // deregister JDBC driver to prevent Tomcat 7 from complaining about memory leaks
        Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver driver = drivers.nextElement();
            try {
                DriverManager.deregisterDriver(driver);
                System.out.println(String.format("Deregistering jdbc driver: %s", driver));
            } catch (SQLException e) {
                System.out.println(String.format("Error deregistering driver %s", driver));
                e.printStackTrace();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:04:18+00:00Added an answer on May 28, 2026 at 5:04 am

    Resolved this, I noticed that the tomcat.jdbc.pool.DataSourceProxy had a close method, so I cast the datasource as a DataSourceProxy, then called closed on it. I no longer get the tomcat memory leak error in the log now.

    SOLUTION:

    import java.sql.Driver;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Enumeration;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.sql.DataSource;
    
    import org.apache.tomcat.jdbc.pool.DataSourceProxy;
    
    public class JdbcPoolListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent myServletContextEvent) {
    
            // initialize jdbc-pool datasource to start out with pooled connections 
            try {
                Context myContext = (Context) new InitialContext().lookup("java:comp/env");
                DataSource myDataSource = (DataSource) myContext.lookup("jdbc/cf");
                myServletContextEvent.getServletContext().setAttribute("JdbcPool", myDataSource);
            } catch (NamingException e) {
                System.out.println("Error initializing jdbc-pool datasource");
                e.printStackTrace();
            }
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent myServletContextEvent) {
    
        // close datasource from proxy?
        ServletContext myServletContext = myServletContextEvent.getServletContext();
            DataSourceProxy myDataSource = (DataSourceProxy) myServletContext.getAttribute("JdbcPool");
            myDataSource.close();       
            myServletContext.removeAttribute("JdbcPool");
    
            // deregister JDBC driver to prevent Tomcat 7 from complaining about memory leaks
            Enumeration<Driver> drivers = DriverManager.getDrivers();
            while (drivers.hasMoreElements()) {
                Driver driver = drivers.nextElement();
                try {
                    DriverManager.deregisterDriver(driver);
                    System.out.println(String.format("Deregistering jdbc driver: %s", driver));
                } catch (SQLException e) {
                    System.out.println(String.format("Error deregistering driver %s", driver));
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement a UITableView of names similar to the built-in Contacts iPhone app
when trying to implement an Aspect, that is responsible for catching and logging a
Trying to implement some nested loops that are spitting out good old nested html
im trying to implement simple secured client server communiction using WCF. when im launching
SetFocus I'm trying implement the above Se Focus code in a Class Library that
I'm trying to make a simple username/password authentication in a Spring Security web app.
Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
Just trying to implement mobFox into my app, but having trouble to make it
I'm trying implement my project in Apache Struts 2 but I'm not very familiar
As part of a web app i am trying to build a registration process.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.