Repository: commons-dbcp Updated Branches: refs/heads/master 70c57b89a -> 75965fd2a
[DBCP-503] org.apache.commons.dbcp2.datasources.InstanceKeyDataSourceFactory.closeAll() does not close all. Project: http://git-wip-us.apache.org/repos/asf/commons-dbcp/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-dbcp/commit/75965fd2 Tree: http://git-wip-us.apache.org/repos/asf/commons-dbcp/tree/75965fd2 Diff: http://git-wip-us.apache.org/repos/asf/commons-dbcp/diff/75965fd2 Branch: refs/heads/master Commit: 75965fd2aaa2ee1c5f34a0ea92b0a444be2cfc85 Parents: 70c57b8 Author: Gary Gregory <[email protected]> Authored: Mon Jun 11 09:13:20 2018 -0600 Committer: Gary Gregory <[email protected]> Committed: Mon Jun 11 09:13:20 2018 -0600 ---------------------------------------------------------------------- src/changes/changes.xml | 3 ++ .../org/apache/commons/dbcp2/ListException.java | 57 ++++++++++++++++++++ .../InstanceKeyDataSourceFactory.java | 34 ++++++++++-- 3 files changed, 89 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/75965fd2/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 73e7933..b318530 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -97,6 +97,9 @@ The <action> type attribute can be add,update,fix,remove. <action dev="ggregory" type="update" issue="DBCP-502" due-to="Gary Gregory"> org.apache.commons.dbcp2.datasources internals should use a char[] instead of a String to store passwords. </action> + <action dev="ggregory" type="update" issue="DBCP-503" due-to="Gary Gregory"> + org.apache.commons.dbcp2.datasources.InstanceKeyDataSourceFactory.closeAll() does not close all. + </action> </release> <release version="2.3.0" date="2018-05-12" description="This is a minor release, including bug fixes and enhancements."> <action dev="pschumacher" type="fix" issue="DBCP-476" due-to="Gary Evesson, Richard Cordova"> http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/75965fd2/src/main/java/org/apache/commons/dbcp2/ListException.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/ListException.java b/src/main/java/org/apache/commons/dbcp2/ListException.java new file mode 100644 index 0000000..540d22e --- /dev/null +++ b/src/main/java/org/apache/commons/dbcp2/ListException.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.dbcp2; + +import java.util.List; + +/** + * An exception wrapping a list of exceptions. + * + * @since 2.4.0 + */ +public class ListException extends Exception { + + private static final long serialVersionUID = 1L; + + private final List<Throwable> exceptionList; + + /** + * Constructs a new exception with the specified detail message. The cause is not initialized, and may subsequently + * be initialized by a call to {@link #initCause}. + * + * @param message + * the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} + * method. + * @param exceptionList + * a list of exceptions. + */ + public ListException(String message, List<Throwable> exceptionList) { + super(message); + this.exceptionList = exceptionList; + } + + /** + * Gets the list of exceptions. + * + * @return the list of exceptions. + */ + public List<Throwable> getExceptionList() { + return exceptionList; + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbcp/blob/75965fd2/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java index ab1b2c6..15e554f 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java @@ -19,9 +19,10 @@ package org.apache.commons.dbcp2.datasources; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; - +import java.util.ArrayList; import java.util.Hashtable; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; @@ -33,9 +34,10 @@ import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; +import org.apache.commons.dbcp2.ListException; + /** - * A JNDI ObjectFactory which creates <code>SharedPoolDataSource</code>s - * or <code>PerUserPoolDataSource</code>s + * A JNDI ObjectFactory which creates <code>SharedPoolDataSource</code>s or <code>PerUserPoolDataSource</code>s * * @since 2.0 */ @@ -71,14 +73,37 @@ abstract class InstanceKeyDataSourceFactory implements ObjectFactory { /** * Closes all pools associated with this class. + * + * @throws Exception + * a {@link ListException} containing all exceptions thrown by {@link InstanceKeyDataSource#close()} + * @see InstanceKeyDataSource#close() + * @see ListException + * @since 2.4.0 throws a {@link ListException} instead of, in 2.3.0 and before, the first exception thrown by + * {@link InstanceKeyDataSource#close()}. */ public static void closeAll() throws Exception { // Get iterator to loop over all instances of this data source. + final List<Throwable> exceptionList = new ArrayList<>(instanceMap.size()); final Iterator<Entry<String, InstanceKeyDataSource>> instanceIterator = instanceMap.entrySet().iterator(); while (instanceIterator.hasNext()) { - instanceIterator.next().getValue().close(); + // Bullet-proof to avoid anything else but problems from InstanceKeyDataSource#close(). + final Entry<String, InstanceKeyDataSource> next = instanceIterator.next(); + if (next != null) { + @SuppressWarnings("resource") + final InstanceKeyDataSource value = next.getValue(); + if (value != null) { + try { + value.close(); + } catch (final Exception e) { + exceptionList.add(e); + } + } + } } instanceMap.clear(); + if (!exceptionList.isEmpty()) { + throw new ListException("Could not close all InstanceKeyDataSource instances.", exceptionList); + } } /** @@ -290,4 +315,3 @@ abstract class InstanceKeyDataSourceFactory implements ObjectFactory { } } } -
