You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by srinath narasimhan <sr...@nuviewinc.com> on 2003/07/03 17:07:28 UTC

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

[DBCP] Jdbc2PoolDataSource closeAll and close

Posted by srinath narasimhan <sr...@nuviewinc.com>.
Has anybody used the closeAll and close methods in the Jdbc2PoolDataSource
class ?
They seem to be not working.

any help/suggestions would be of help.

Thanks

Srinath.


Re[2]: [DBCP] connection pool shutdown

Posted by Anton Tagunov <at...@mail.cnt.ru>.
Hello, Srinath!

sn> But I don't want the context to be reloaded as well.
sn> I just want to terminate all the connections to the database.
sn> I am using sql server, if I want to rebuild user security or restore
sn> database,
sn> there should not be any connection to the database. So I just need to close
sn> all the connections.

well, I doubt it can be done with fetching the datasource
via JNDI, but you could

a) make all the access to the pool via the ServletContext
b) implement a specialized managing servlet
   that would act on two uri-s: /down and /up

   on /up it would
   1. fetch the DataSourcePreviously bound into ServletContext
   2. call .close() on it
   3. create a new BasicDataSource
   4. bind it into the ServletContext

   on /down it would
   1. unbind the datasource from ServletContext, call
   2. call .close() on it

c) implement a ServletContextListener that would
   on servlet application startup create a new DataSource
   and bind it into the ServletContext

d) all the components using the DataSource should be
   prepared to handle the situation when they find
   no BasicDataSource in the ServletContext
   (since it may have been /down-ed)

   alternatively (if your servlet api < 2.3)
   you may use not a ContextListener, but
   the same servlet that handles /up and /down
   could on its startup do it

e) you should properly safeguard access to these
   /up and /down url-s to prevent malicious user
   from bringing your database access down.

   at the very minimum you would probably want
   to shield them with username/password in
   the normal way it is done with Tomcat
   (or Apache httpd if you're running via it)

Please consider sending your control servlet back
to the list after you have this up and running.
   
-Anton


RE: [DBCP] connection pool shutdown

Posted by srinath narasimhan <sr...@nuviewinc.com>.
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


RE: [DBCP] connection pool shutdown

Posted by Michael Holly <mh...@talisentech.com>.
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.