commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "srinath narasimhan" <srin...@nuviewinc.com>
Subject RE: [DBCP] connection pool shutdown
Date Thu, 03 Jul 2003 20:24:12 GMT
Thanks Holly,
But I don't want the context to be reloaded as well.
I just want to terminate all the connections to the database.
I am using sql server, if I want to rebuild user security or restore
database,
there should not be any connection to the database. So I just need to close
all the connections.

Thanks again.


-----Original Message-----
From: Michael Holly [mailto:mholly@talisentech.com]
Sent: Thursday, July 03, 2003 16:20
To: Jakarta Commons Users List
Subject: RE: [DBCP] connection pool shutdown


Srinath

I just had the same problem.  I configure my connections in a context.xml
file.  Create a Context Listener and you can kill the connections in the
contextDestroyed() method.  This runs when the app is removed from tomcat.
The contextInitialized() is a good place to configure/initalize "Context"
resources.

In my web.xml I had to add the following lines to start the ContextListener.

<!-- CONTEXT LISTENER -->
    <listener>
        <listener-class>net.talisen.tsr.ContextListener</listener-class>
    </listener>

Here is my source for my listener.


/**
 *  The listener runs when the app is started and shutdown
 *
 * @author  Michael Holly et. al
 * created  June 30, 2003
 */
package net.talisen.tsr;


import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;


import org.apache.commons.dbcp.BasicDataSource;
import javax.servlet.*;
import org.apache.log4j.Logger;
import java.util.ResourceBundle;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
import java.util.*;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
import org.apache.log4j.PropertyConfigurator;

public final class ContextListener
implements ServletContextListener
{

   //get a logger
   Logger log = Logger.getLogger(ContextListener.class);


   private InitialContext initialContext = null;
   private Context namingContext = null;
   private ServletContext context = null;

   public void contextInitialized (ServletContextEvent servletContextEvent)
   {

      context = servletContextEvent.getServletContext ();
      try
      {

         log.info("Initializing logging");
         // configure the Log4j system
         String file = new String( "/WEB-INF/classes/log4j.properties" );
         URL url = context.getResource(file);
         PropertyConfigurator.configure( url );
         System.out.println("Log4j Properties @ " + url.toString() );


         log.info("Cataloging Context Resources");
         initialContext = new InitialContext();

         namingContext = (Context) initialContext.lookup("java:comp/env");

         DataSource ds1 =
(DataSource)namingContext.lookup("jdbc/oracle_tsr");

         log.info("oracle_tsr connection pool cataloged");

         context.setAttribute("dataSource1", ds1);

      }
      catch (NamingException ne)
      {
         log.error("Couldn't create context attribute: " + ne.getMessage
());
         ne.printStackTrace();
      }
      catch (Exception e)
      {
         log.error("Couldn't create context attribute: " + e.getMessage ());
         e.printStackTrace();
      }
   }

   public void contextDestroyed (ServletContextEvent servletContextEvent)
   {

      DataSource ds1 = ((DataSource) context.getAttribute("dataSource1"));

      try
      {

         log.info("Cleaning up Context Resources");

         if (ds1 instanceof org.apache.commons.dbcp.BasicDataSource) {
            log.info("Found oracle_tsr connection pool " + ds1.toString());
            ((org.apache.commons.dbcp.BasicDataSource) ds1).close();
            ds1 = null;
         }
         log.info("Removed oracle_tsr connection ");

         context.removeAttribute ("dataSource1");
      }
      catch (Exception e)
      {
         log.error("Error destroying Context: " + e.getMessage ());
         e.printStackTrace();
      }
      finally
      {

System.out.println("########################################################
###########################################");

System.out.println("########################################################
###########################################");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
         System.out.println("");
      }
   }
}


Hope this helps

Michael


-----Original Message-----
From: srinath narasimhan [mailto:srinath@nuviewinc.com]
Sent: Thursday, July 03, 2003 10:07 AM
To: commons-user@jakarta.apache.org
Subject: [DBCP] connection pool shutdown


Is there any way to close all the connections in the pool at some point,
without having to restart tomcat.
Right now, whenever I need to close connections, I have to restart tomcat so
that all the connections in the pool will be destoryed.

Thanks in advance.

Srinath.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message