This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/master by this push: new 8066eb5 Improve fix for https://bz.apache.org/bugzilla/show_bug.cgi?id=63210 8066eb5 is described below commit 8066eb5f5e5459fed5827546b1b60d434f4a1090 Author: Mark Thomas <ma...@apache.org> AuthorDate: Tue Mar 5 10:50:19 2019 +0000 Improve fix for https://bz.apache.org/bugzilla/show_bug.cgi?id=63210 Close AutoCloseable DataSources by default. Covers DBCP 2 and custom factories as well as any other DataSources that need to be closed. --- java/org/apache/catalina/core/NamingContextListener.java | 16 +++++++--------- webapps/docs/config/context.xml | 16 ++++++++-------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/java/org/apache/catalina/core/NamingContextListener.java b/java/org/apache/catalina/core/NamingContextListener.java index 5c124c8..f6f2132 100644 --- a/java/org/apache/catalina/core/NamingContextListener.java +++ b/java/org/apache/catalina/core/NamingContextListener.java @@ -1011,23 +1011,21 @@ public class NamingContextListener if (("javax.sql.DataSource".equals(ref.getClassName()) || "javax.sql.XADataSource".equals(ref.getClassName())) && resource.getSingleton()) { - String factory = (String) resource.getProperty("factory"); - if ((factory == null || factory.equals(Constants.DBCP_DATASOURCE_FACTORY)) && - !resource.getCloseMethodConfigured()) { - // Using Tomcat's built-in factory (DBCP2) and DBCP2 DataSources - // require an explicit close - resource.setCloseMethod("close"); - } + Object actualResource = null; try { ObjectName on = createObjectName(resource); - Object actualResource = envCtx.lookup(resource.getName()); + actualResource = envCtx.lookup(resource.getName()); Registry.getRegistry(null, null).registerComponent(actualResource, on, null); objectNames.put(resource.getName(), on); } catch (Exception e) { log.warn(sm.getString("naming.jmxRegistrationFailed", e)); } + // Bug 63210. DBCP2 DataSources require an explicit close. This goes + // further and cleans up and AutoCloseable DataSource by default. + if (actualResource instanceof AutoCloseable && !resource.getCloseMethodConfigured()) { + resource.setCloseMethod("close"); + } } - } diff --git a/webapps/docs/config/context.xml b/webapps/docs/config/context.xml index 27a49a4..306568c 100644 --- a/webapps/docs/config/context.xml +++ b/webapps/docs/config/context.xml @@ -1212,14 +1212,14 @@ it is no longer required. This is intended to speed up clean-up of resources that would otherwise happen as part of garbage collection. This attribute is ignored if the <code>singleton</code> attribute is - false. If not specified, no default is defined and no close method will - be called.</p> - <p>For Apache Commons DBCP 2 and Apache Tomcat JDBC connection pools - you can use <code>closeMethod="close"</code>. Note that Apache Commons - DBCP 2 requires this to be set for a clean shutdown. When using the - default Tomcat connection pool (based on DBCP 2) Tomcat will set this - attribute automatically unless it is explictly set to the empty - string.</p> + false.</p> + <p>For <code>javax.sql.DataSource</code> and + <code>javax.sql.XADataSource</code> resources that implement + <code>AutoCloseable</code> such as Apache Commons DBCP 2 and the default + Apache Tomcat connection pool, this attribute is defaults to + <code>close</code>. This may be disabled by setting the attribute to the + empty string. For all other resource types no default is defined and no + close method will be called by default.</p> </attribute> <attribute name="description" required="false"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org