-----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: [email protected]
For additional commands, e-mail: [email protected]