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 4099770
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:28:45+00:00 2026-05-20T20:28:45+00:00

Update: Apparently Tomcat, starting with 7.0.11, closes the DataSource for you, so it’s not

  • 0

Update: Apparently Tomcat, starting with 7.0.11, closes the DataSource for you, so it’s not available in the webapp’s contextDestroyed. See: https://issues.apache.org/bugzilla/show_bug.cgi?id=25060

Hi,

I’m using Spring 3.0 and Java 1.6.

If I get a data source this way:

<bean id="dataSource" class="my.data.Source" destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:home"/>
    <property name="username" value="user"/>
    <property name="password" value="pw"/>
</bean>

then the data source is closed when the bean is destroyed.

If I get the data source like this:

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/db" />

then do I have to explicitly close the data source in my contextDestroyed listener?

Thanks,

Paul

  • 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-20T20:28:45+00:00Added an answer on May 20, 2026 at 8:28 pm

    I disagree. I would add a listener to your web.xml and implement the contextDestroyed() method. This method will get called by your web container/app server when the web app is destroyed or undeployed. Within the contextDestroyed(), I would close the datasource.

    inside the web.xml

    <listener>
       <listener-class>util.myApplicationWatcher</listener-class>
    </listener>
    

    The code:

    package util;
    
    public class myApplicationWatcher implementes ServletContextListener
    {
      public void contextInitialized(ServletContextEvent cs)
      {
          // This web application is getting started
    
          // Initialize connection pool here by running a query
          JdbcTemplate jt = new JdbcTemplate(Dao.getDataSource() );
          jt.queryForInt("Select count(col1) from some_table");
      }
    
      public void contextDestroyed(ServeletContextEvent ce)
      {
          // This web application is getting undeployed or destroyed 
    
          // close the connection pool
          Dao.closeDataSource();
      }
    }
    
    public class Dao
    {
      private static DataSource ds;
      private static bDataSourceInitialized=false;
      private static void initializeDataSource() throws Exception
      {
        InitialContext initial = new InitialContext();
    
        ds = (DataSource) initial.lookup(TOMCAT_JNDI_NAME);
    
        if (ds.getConnection() == null)
        {
          throw new RuntimeException("I failed to find the TOMCAT_JNDI_NAME");
        }
    
        bDataSourceInitialized=true;
      }
    
      public static void closeDataSource() throws Exception
      {
        // Cast my DataSource class to a c3po connection pool class
        // since c3po is what I use in my context.xml
        ComboPooledDataSource cs = (ComboPooledDatasource) ds;
    
        // close this connection pool
        cs.close();
      }
    
      public static DataSource getDataSource() throws Exception
      {
        if (bDataSourceInitialized==false)
        {
          initializeDataSource();
        }
    
        return(ds);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: Apparently these are control characters, not Unicode characters. I'm trying to parse an
Update: Solved, with code I got it working, see my answer below for the
Update : Looks like the query does not throw any timeout. The connection is
Update Apparently the solution to this is to use yet another configuration parameter to
UPDATE: IParcelable apparently cannot currently be implemented in Mono for Android . In the
UPDATE: apparently, the issue I've been experiencing only affects the first element in the
So apparently Microsoft does not have a Months Enum on their Date structure. What
UPDATE: I actually found the solution myself, see below. In R I want to
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: giving a much more thorough example. The first two solutions offered were right

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.