From bd402d61a667bcddf214a06e6458d45e83c2d8f3 Mon Sep 17 00:00:00 2001 From: markt Date: Wed, 30 Mar 2011 13:35:12 +0000 Subject: [PATCH] Implment Filip's idea for a configurable close method git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1086950 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/catalina/deploy/ContextResource.java | 18 +++++++++++ .../apache/catalina/deploy/LocalStrings.properties | 6 ++-- .../apache/catalina/deploy/NamingResources.java | 36 ++++++++++++---------- webapps/docs/changelog.xml | 4 +++ webapps/docs/config/context.xml | 7 +++++ 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/java/org/apache/catalina/deploy/ContextResource.java b/java/org/apache/catalina/deploy/ContextResource.java index 237e5832f..dbe85268d 100644 --- a/java/org/apache/catalina/deploy/ContextResource.java +++ b/java/org/apache/catalina/deploy/ContextResource.java @@ -80,6 +80,24 @@ public class ContextResource extends ResourceBase { this.singleton = singleton; } + + /** + * The name of the zero argument method to be called when the resource is + * no longer required to clean-up resources. This method must only speed up + * the clean-up of resources that would otherwise happen via garbage + * collection. + */ + private String closeMethod = "close"; + + public String getCloseMethod() { + return closeMethod; + } + + public void setCloseMethod(String closeMethod) { + this.closeMethod = closeMethod; + } + + // --------------------------------------------------------- Public Methods diff --git a/java/org/apache/catalina/deploy/LocalStrings.properties b/java/org/apache/catalina/deploy/LocalStrings.properties index 5f2b8cd5d..c22575719 100644 --- a/java/org/apache/catalina/deploy/LocalStrings.properties +++ b/java/org/apache/catalina/deploy/LocalStrings.properties @@ -43,9 +43,9 @@ webxml.unrecognisedPublicId=The public ID [{0}] did not match any of the known p webXml.version.nfe=Unable to parse [{0}] from the version string [{1}]. This component of the version string will be ignored. webXml.wrongFragmentName=Used a wrong fragment name {0} at web.xml absolute-ordering tag! -namingResources.cleanupCloseFailed=Failed to invoke close method for resource [{0}] in container [{1}] so no cleanup was performed for that resource -namingResources.cleanupCloseSecurity=Unable to retrieve close method for resource [{0}] in container [{1}] so no cleanup was performed for that resource -namingResources.cleanupNoClose=Resource [{0}] in container [{1}] does not have a close method so no cleanup was performed for that resource +namingResources.cleanupCloseFailed=Failed to invoke method [{0}] for resource [{1}] in container [{2}] so no cleanup was performed for that resource +namingResources.cleanupCloseSecurity=Unable to retrieve method [{0}] for resource [{1}] in container [{2}] so no cleanup was performed for that resource +namingResources.cleanupNoClose=Resource [{0}] in container [{1}] does not have a [{2}] method so no cleanup was performed for that resource namingResources.cleanupNoContext=Failed to retrieve JNDI naming context for container [{0}] so no cleanup was performed for that container namingResources.cleanupNoResource=Failed to retrieve JNDI resource [{0}] for container [{1}] so no cleanup was performed for that resource namingResources.mbeanCreateFail=Failed to create MBean for naming resource [{0}] diff --git a/java/org/apache/catalina/deploy/NamingResources.java b/java/org/apache/catalina/deploy/NamingResources.java index 726a68076..58a66c06a 100644 --- a/java/org/apache/catalina/deploy/NamingResources.java +++ b/java/org/apache/catalina/deploy/NamingResources.java @@ -28,7 +28,6 @@ import java.util.HashMap; import java.util.Hashtable; import javax.naming.NamingException; -import javax.sql.DataSource; import org.apache.catalina.Container; import org.apache.catalina.Context; @@ -981,54 +980,57 @@ public class NamingResources extends LifecycleMBeanBase implements Serializable return; } for (ContextResource cr: resources.values()) { - if (DataSource.class.getName().equals(cr.getType())) { + String closeMethod = cr.getCloseMethod(); + if (closeMethod != null && closeMethod.length() > 0) { String name = cr.getName(); - DataSource ds; + Object resource; try { - ds = (DataSource) ctxt.lookup(name); + resource = ctxt.lookup(name); } catch (NamingException e) { log.warn(sm.getString("namingResources.cleanupNoResource", cr.getName(), container), e); continue; } - cleanUp(ds, name); + cleanUp(resource, name, cr.getCloseMethod()); } } } /** - * Closing a database connection pool will close it's open connections. This + * Clean up a resource by calling the defined close method. For example, + * closing a database connection pool will close it's open connections. This * will happen on GC but that leaves db connections open that may cause * issues. - * @param ds The DataSource to close. + * + * @param resource The resource to close. */ - private void cleanUp(DataSource ds, String name) { + private void cleanUp(Object resource, String name, String closeMethod) { // Look for a zero-arg close() method Method m = null; try { - m = ds.getClass().getMethod("close", (Class[]) null); + m = resource.getClass().getMethod(closeMethod, (Class[]) null); } catch (SecurityException e) { - log.debug(sm.getString("namingResources.cleanupCloseSecurity", name, - container)); + log.debug(sm.getString("namingResources.cleanupCloseSecurity", + closeMethod, name, container)); return; } catch (NoSuchMethodException e) { - log.debug(sm.getString("namingResources.cleanupNoClose", name, - container)); + log.debug(sm.getString("namingResources.cleanupNoClose", + name, container, closeMethod)); return; } if (m != null) { try { - m.invoke(ds, (Object[]) null); + m.invoke(resource, (Object[]) null); } catch (IllegalArgumentException e) { log.warn(sm.getString("namingResources.cleanupCloseFailed", - name, container), e); + closeMethod, name, container), e); } catch (IllegalAccessException e) { log.warn(sm.getString("namingResources.cleanupCloseFailed", - name, container), e); + closeMethod, name, container), e); } catch (InvocationTargetException e) { log.warn(sm.getString("namingResources.cleanupCloseFailed", - name, container), e); + closeMethod, name, container), e); } } } diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 3c73025c6..6f702d23b 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -115,6 +115,10 @@ close on a JNDI resource while it was still available to the application. (markt) + + Provide a configuration option that lets the close method to be used for + a JNDI Resource to be defined by the user. (markt) + diff --git a/webapps/docs/config/context.xml b/webapps/docs/config/context.xml index 5996763d5..678c17265 100644 --- a/webapps/docs/config/context.xml +++ b/webapps/docs/config/context.xml @@ -1013,6 +1013,13 @@ application uses a <resource-env-ref> instead.

+ +

Name of zero-argument method to call on the resource when it is no + longer required to spped up clean-up of resources that would otherwise + happen as part of garbage collection. If not specificed, the default + value of close is used.

+
+

Optional, human-readable description of this resource.

-- 2.11.0