John
I have created a "Context Listener" class that catalogs the connections
configured in the connection pool when the application starts up and
then listens for when the application or the tomcat server is shut down.
This class tests to see if the pool has any live connections and closes
them before allowing the application to close. Notice I have two
different pools.
Here is the class listing
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
{
System.out.println(" \n\n Intializing Context");
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 ds2 =
(DataSource)namingContext.lookup("jdbc/oracle_app");
DataSource ds1 =
(DataSource)namingContext.lookup("jdbc/mysql_app");
context.setAttribute("dataSource1", ds1);
context.setAttribute("dataSource2", ds2);
log.info("mysql_ftpApp connection pool cataloged");
log.info("oracle_talisen connection pool cataloged");
}
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"));
DataSource ds2 = ((DataSource)
context.getAttribute("dataSource2"));
try
{
log.info("Cleaning up Context Resources");
if (ds1 instanceof org.apache.commons.dbcp.BasicDataSource) {
log.info("Found mysql_app connection pool " +
ds1.toString());
int active = ((org.apache.commons.dbcp.BasicDataSource)
ds1).getNumActive();
log.info( active + " connection(s)");
int idle = ((org.apache.commons.dbcp.BasicDataSource)
ds1).getNumIdle();
log.info( idle + " connection(s)");
if ((active + idle) > 0 ) {
((org.apache.commons.dbcp.BasicDataSource) ds1).close();
log.info("Data source closed ");
}
ds1 = null;
log.info("Releasing context var");
}
log.info("Removed mysql_App connection ");
if (ds2 instanceof org.apache.commons.dbcp.BasicDataSource) {
log.info("Found oracle_app connection pool " +
ds2.toString() );
int active = ((org.apache.commons.dbcp.BasicDataSource)
ds2).getNumActive();
log.info( active + " connection(s)");
int idle = ((org.apache.commons.dbcp.BasicDataSource)
ds2).getNumIdle();
log.info( idle + " connection(s)");
if ((active + idle) > 0 ) {
((org.apache.commons.dbcp.BasicDataSource) ds2).close();
log.info("Data source closed ");
}
ds2 = null;
log.info("Releasing context var");
}
log.info("Removed oracle_app connection");
context.removeAttribute ("dataSource1");
context.removeAttribute ("dataSource2");
}
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("");
}
}
}
Here is the entry into the web.xml
<!-- CONTEXT LISTENER -->
<listener>
<listener-class>net.pulse.ftpApp.ContextListener</listener-class>
</listener>
Hope this helps
Michael
-----Original Message-----
From: John Zoetebier [mailto:[EMAIL PROTECTED]
Sent: Saturday, November 08, 2003 11:32 PM
To: Jakarta Commons Users List
Subject: Re: DBCP question: what is closing the actual database
connection ?
On Sun, 9 Nov 2003 03:56:15 +0200, Schalk <[EMAIL PROTECTED]> wrote:
> John
>
> In basic situation you will have:
> Class.forName(DRIVER);
> con = DriverManager.getConnection(URL,USER,PASS);
>
> At the end you will have:
> finally {
> try {
> if (con != null)
> con.close();
> } catch (SQLException ex) {
> out.println("\nERROR:------ SQLException -----\n");
> out.println("Message: " + ex.getMessage());
> out.println("SQLState: " + ex.getSQLState());
> out.println("ErrorCode :" + ex.getErrorCode());
> }
> }
>
The result of con.close() is that the connection is returned to the
connection pool.
This is the purpose of connection pooling: reuse of a connection to
increase performance.
However at program shutdown something needs to close the "real"
connection
in the connection pool.
My question is:
- is closing of connections in the pool handled automatically by Jakarta
Commons DBCP ?
- or do I have do something manaully to close the connections in the
pool ?
--
John Zoetebier
Web site: http://www.transparent.co.nz
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]