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:[EMAIL PROTECTED]
Sent: Thursday, July 03, 2003 10:07 AM
To: [EMAIL PROTECTED]
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]