This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
The following commit(s) were added to refs/heads/master by this push:
new 609cdc81 Use try-with-resources
609cdc81 is described below
commit 609cdc81602d97b84be58a2b0fad6e043f068d93
Author: Gary Gregory <[email protected]>
AuthorDate: Tue Jun 9 16:53:18 2026 -0400
Use try-with-resources
- Don't hide exceptions on internal close()
- Use generics
- Remove now unnecessary @SuppressWarnings
---
.../datasources/InstanceKeyDataSourceFactory.java | 25 ++++++++--------------
.../datasources/PerUserPoolDataSourceFactory.java | 16 ++++++--------
2 files changed, 16 insertions(+), 25 deletions(-)
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 81a8971e..916f2138 100644
---
a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
+++
b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSourceFactory.java
@@ -24,7 +24,6 @@ import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
-import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import javax.naming.Context;
@@ -74,22 +73,16 @@ abstract class InstanceKeyDataSourceFactory implements
ObjectFactory {
/**
* Deserializes the provided byte array to create an object.
*
- * @param data
- * Data to deserialize to create the configuration parameter.
- *
+ * @param <T> The type of the object to be deserialized.
+ * @param data Data to deserialize to create the configuration parameter.
* @return The Object created by deserializing the data.
- * @throws ClassNotFoundException
- * If a class cannot be found during the deserialization of a
configuration parameter.
- * @throws IOException
- * If an I/O error occurs during the deserialization of a
configuration parameter.
+ * @throws ClassNotFoundException If a class cannot be found during the
deserialization of a configuration parameter.
+ * @throws IOException If an I/O error occurs during the
deserialization of a configuration parameter.
*/
- protected static final Object deserialize(final byte[] data) throws
IOException, ClassNotFoundException {
- ObjectInputStream in = null;
- try {
- in = new ObjectInputStream(new ByteArrayInputStream(data));
- return in.readObject();
- } finally {
- Utils.closeQuietly(in);
+ @SuppressWarnings("unchecked") // call site must ensure that the type is
correct
+ protected static final <T> T deserialize(final byte[] data) throws
IOException, ClassNotFoundException {
+ try (ObjectInputStream in = new ObjectInputStream(new
ByteArrayInputStream(data))) {
+ return (T) in.readObject();
}
}
@@ -216,7 +209,7 @@ abstract class InstanceKeyDataSourceFactory implements
ObjectFactory {
refAddr = ref.get("jndiEnvironment");
if (hasContent(refAddr)) {
final byte[] serialized = (byte[]) refAddr.getContent();
- ikds.setJndiEnvironment((Properties) deserialize(serialized));
+ ikds.setJndiEnvironment(deserialize(serialized));
}
refAddr = ref.get("loginTimeout");
diff --git
a/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java
b/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java
index 8677419e..2e0fa896 100644
---
a/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java
+++
b/src/main/java/org/apache/commons/dbcp2/datasources/PerUserPoolDataSourceFactory.java
@@ -18,7 +18,6 @@ package org.apache.commons.dbcp2.datasources;
import java.io.IOException;
import java.time.Duration;
-import java.util.Map;
import javax.naming.RefAddr;
import javax.naming.Reference;
@@ -32,13 +31,12 @@ public class PerUserPoolDataSourceFactory extends
InstanceKeyDataSourceFactory {
private static final String PER_USER_POOL_CLASSNAME =
PerUserPoolDataSource.class.getName();
/**
- * Constructs a new instance.
+ * Constructs a new instance.Ωø∂
*/
public PerUserPoolDataSourceFactory() {
// empty
}
- @SuppressWarnings("unchecked") // Avoid warnings on deserialization
@Override
protected InstanceKeyDataSource getNewInstance(final Reference ref) throws
IOException, ClassNotFoundException {
final PerUserPoolDataSource pupds = new PerUserPoolDataSource();
@@ -60,37 +58,37 @@ public class PerUserPoolDataSourceFactory extends
InstanceKeyDataSourceFactory {
refAddr = ref.get("perUserDefaultAutoCommit");
if (refAddr != null && refAddr.getContent() != null) {
final byte[] serialized = (byte[]) refAddr.getContent();
- pupds.setPerUserDefaultAutoCommit((Map<String, Boolean>)
deserialize(serialized));
+ pupds.setPerUserDefaultAutoCommit(deserialize(serialized));
}
refAddr = ref.get("perUserDefaultTransactionIsolation");
if (refAddr != null && refAddr.getContent() != null) {
final byte[] serialized = (byte[]) refAddr.getContent();
- pupds.setPerUserDefaultTransactionIsolation((Map<String, Integer>)
deserialize(serialized));
+
pupds.setPerUserDefaultTransactionIsolation(deserialize(serialized));
}
refAddr = ref.get("perUserMaxTotal");
if (refAddr != null && refAddr.getContent() != null) {
final byte[] serialized = (byte[]) refAddr.getContent();
- pupds.setPerUserMaxTotal((Map<String, Integer>)
deserialize(serialized));
+ pupds.setPerUserMaxTotal(deserialize(serialized));
}
refAddr = ref.get("perUserMaxIdle");
if (refAddr != null && refAddr.getContent() != null) {
final byte[] serialized = (byte[]) refAddr.getContent();
- pupds.setPerUserMaxIdle((Map<String, Integer>)
deserialize(serialized));
+ pupds.setPerUserMaxIdle(deserialize(serialized));
}
refAddr = ref.get("perUserMaxWaitMillis");
if (refAddr != null && refAddr.getContent() != null) {
final byte[] serialized = (byte[]) refAddr.getContent();
- pupds.setPerUserMaxWaitMillis((Map<String, Long>)
deserialize(serialized));
+ pupds.setPerUserMaxWaitMillis(deserialize(serialized));
}
refAddr = ref.get("perUserDefaultReadOnly");
if (refAddr != null && refAddr.getContent() != null) {
final byte[] serialized = (byte[]) refAddr.getContent();
- pupds.setPerUserDefaultReadOnly((Map<String, Boolean>)
deserialize(serialized));
+ pupds.setPerUserDefaultReadOnly(deserialize(serialized));
}
return pupds;
}