Return-Path: Delivered-To: apmail-tomcat-users-archive@www.apache.org Received: (qmail 9948 invoked from network); 6 Aug 2009 14:41:02 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 6 Aug 2009 14:41:02 -0000 Received: (qmail 11716 invoked by uid 500); 6 Aug 2009 14:41:04 -0000 Delivered-To: apmail-tomcat-users-archive@tomcat.apache.org Received: (qmail 11667 invoked by uid 500); 6 Aug 2009 14:41:04 -0000 Mailing-List: contact users-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Users List" Delivered-To: mailing list users@tomcat.apache.org Received: (qmail 11656 invoked by uid 99); 6 Aug 2009 14:41:04 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Aug 2009 14:41:04 +0000 X-ASF-Spam-Status: No, hits=1.2 required=10.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [76.96.62.24] (HELO QMTA02.westchester.pa.mail.comcast.net) (76.96.62.24) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 06 Aug 2009 14:40:52 +0000 Received: from OMTA19.westchester.pa.mail.comcast.net ([76.96.62.98]) by QMTA02.westchester.pa.mail.comcast.net with comcast id QzDE1c00627AodY522gYoh; Thu, 06 Aug 2009 14:40:32 +0000 Received: from [192.168.1.101] ([69.143.128.194]) by OMTA19.westchester.pa.mail.comcast.net with comcast id R2jM1c0094BnRt93f2jN1K; Thu, 06 Aug 2009 14:43:22 +0000 Message-ID: <4A7AEB5D.1070809@christopherschultz.net> Date: Thu, 06 Aug 2009 10:40:29 -0400 From: Christopher Schultz User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.1) Gecko/20090715 Thunderbird/3.0b3 MIME-Version: 1.0 To: Tomcat Users List Subject: Re: Right way to close database connection pool References: <4A79B6BD.10302@gmail.com> <4A79BCFC.6050401@yale.edu> <4A79F35E.7090909@hanik.com> In-Reply-To: <4A79F35E.7090909@hanik.com> X-Enigmail-Version: 0.96a Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Virus-Checked: Checked by ClamAV on apache.org -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Filip, On 8/5/2009 5:02 PM, Filip Hanik - Dev Lists wrote: > and if you don't want to depend on a specific implementation, use > reflection Since this is a "missing feature", would it be reasonable for Tomcat to provide a "sample" filter that webapps can use to shut down their JNDI Resource pools? I would be happy to write such a filter. In fact, here it is: import java.sql.SQLException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import javax.servlet.ServletContext; import javax.servlet.ServletContextListener; import javax.servlet.ServletContextEvent; /** * A listener to shut down the JNDI DataSource created by Tomcat * (but not automatically cleaned up). * * @author Chris Schultz * @version $Revision: 1.25 $ $Date: 2008-05-27 21:11:53 $ */ public class JNDIDataSourceShutdownListener implements ServletContextListener { private String _dataSourcePath; public void contextInitialized(ServletContextEvent e) { ServletContext application = e.getServletContext(); _dataSourcePath = application.getInitParameter("JNDIDataSourceName"); if(!_dataSourcePath.startsWith("java:")) _dataSourcePath = "java:/comp/env/" + _dataSourcePath; } /** * Attempts to close the DataSource */ public void contextDestroyed(ServletContextEvent e) { ServletContext application = e.getServletContext(); try { Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup(_dataSourcePath); if(null == ds) { application.log(getClass().getName() + ": No DataSource found at " + _dataSourcePath); } else { application.log(getClass().getName() + ": Closing DataSource " + _dataSourcePath); close(ds, application); application.log(getClass().getName() + ": Closed DataSource " + _dataSourcePath); } } catch (NamingException ne) { application.log(getClass().getName() + ": Unable to locate DataSource at " + _dataSourcePath, ne); } } private void close(DataSource ds, ServletContext application) { try { Method closeMethod = ds.getClass().getMethod("close", null); if(null == closeMethod) throw new NoSuchMethodException(ds.getClass().getName() + ".close()"); closeMethod.invoke(ds, null); } catch (Exception e) { application.log(getClass().getName() + ": Cannot close DataSource", e); } } } - -chris -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkp6610ACgkQ9CaO5/Lv0PBswgCfYf0tIVZrhkmehhYQS4d3CJ01 nUMAoLM5VYy1PwDC0iNReS1yIdPIm6XN =vTks -----END PGP SIGNATURE----- --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org For additional commands, e-mail: users-help@tomcat.apache.org