You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/08/20 21:52:52 UTC

[commons-dbcp] branch master updated: org.mockito:mockito-core 3.11.2 -> 3.12.1.

This is an automated email from the ASF dual-hosted git repository.

ggregory 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 739b904  org.mockito:mockito-core 3.11.2 -> 3.12.1.
739b904 is described below

commit 739b90420857b398a7b1a413f17083ce6ff8299d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Aug 20 17:52:50 2021 -0400

    org.mockito:mockito-core 3.11.2 -> 3.12.1.
    
    Add local @SuppressWarnings.
    Fix compiler warnings.
    Use try-with-resources.
    Refactor KeyedCPDSConnectionFactory ctor.
---
 pom.xml                                            |   9 +-
 src/changes/changes.xml                            |   3 +
 .../datasources/KeyedCPDSConnectionFactory.java    |  21 +--
 .../dbcp2/cpdsadapter/TestDriverAdapterCPDS.java   |   4 +-
 .../datasources/TestCPDSConnectionFactory.java     |   2 +-
 .../TestKeyedCPDSConnectionFactory.java            | 167 +++++++++---------
 .../datasources/TestSharedPoolDataSource.java      | 192 ++++++++++-----------
 7 files changed, 196 insertions(+), 202 deletions(-)

diff --git a/pom.xml b/pom.xml
index a87c4d9..753c17f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -205,7 +205,14 @@
     <dependency>
       <groupId>org.mockito</groupId>
       <artifactId>mockito-core</artifactId>
-      <version>3.11.2</version>
+      <version>3.12.1</version>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.12.0</version>
       <scope>test</scope>
     </dependency>
 
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4f8be18..22f713b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -125,6 +125,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action dev="ggregory" type="update" due-to="Dependabot">
         Bump spotbugs from 4.3.0 to 4.4.0 #124.
       </action>
+      <action dev="ggregory" type="update" due-to="Gary Gregory">
+        Bump org.mockito:mockito-core 3.11.2 -> 3.12.1.
+      </action>
     </release>
     <release version="2.9.0" date="2021-07-30" description="This is a minor release, including bug fixes and enhancements.">
       <!-- ADDS -->
diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
index ce384c6..1ff2fde 100644
--- a/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
+++ b/src/main/java/org/apache/commons/dbcp2/datasources/KeyedCPDSConnectionFactory.java
@@ -108,11 +108,8 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey
      */
     @Deprecated
     public KeyedCPDSConnectionFactory(final ConnectionPoolDataSource cpds, final String validationQuery,
-            final int validationQueryTimeoutSeconds, final boolean rollbackAfterValidation) {
-        this.cpds = cpds;
-        this.validationQuery = validationQuery;
-        this.validationQueryTimeoutDuration = Duration.ofSeconds(validationQueryTimeoutSeconds);
-        this.rollbackAfterValidation = rollbackAfterValidation;
+        final int validationQueryTimeoutSeconds, final boolean rollbackAfterValidation) {
+        this(cpds, validationQuery, Duration.ofSeconds(validationQueryTimeoutSeconds), rollbackAfterValidation);
     }
 
     @Override
@@ -347,22 +344,22 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey
             return false;
         }
         boolean valid = false;
-        final PooledConnection pconn = pooledObject.getObject().getPooledConnection();
+        final PooledConnection pooledConn = pooledObject.getObject().getPooledConnection();
         Connection conn = null;
-        validatingSet.add(pconn);
+        validatingSet.add(pooledConn);
         if (null == validationQuery) {
             Duration timeoutDuration = validationQueryTimeoutDuration;
             if (timeoutDuration.isNegative()) {
                 timeoutDuration = Duration.ZERO;
             }
             try {
-                conn = pconn.getConnection();
+                conn = pooledConn.getConnection();
                 valid = conn.isValid((int) timeoutDuration.getSeconds());
             } catch (final SQLException e) {
                 valid = false;
             } finally {
                 Utils.closeQuietly((AutoCloseable) conn);
-                validatingSet.remove(pconn);
+                validatingSet.remove(pooledConn);
             }
         } else {
             Statement stmt = null;
@@ -371,9 +368,9 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey
             // before another one can be requested and closing it will
             // generate an event. Keep track so we know not to return
             // the PooledConnection
-            validatingSet.add(pconn);
+            validatingSet.add(pooledConn);
             try {
-                conn = pconn.getConnection();
+                conn = pooledConn.getConnection();
                 stmt = conn.createStatement();
                 rset = stmt.executeQuery(validationQuery);
                 valid = rset.next();
@@ -386,7 +383,7 @@ class KeyedCPDSConnectionFactory implements KeyedPooledObjectFactory<UserPassKey
                 Utils.closeQuietly((AutoCloseable) rset);
                 Utils.closeQuietly((AutoCloseable) stmt);
                 Utils.closeQuietly((AutoCloseable) conn);
-                validatingSet.remove(pconn);
+                validatingSet.remove(pooledConn);
             }
         }
         return valid;
diff --git a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java
index 1d74e08..4c5c93d 100644
--- a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java
+++ b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java
@@ -218,7 +218,9 @@ public class TestDriverAdapterCPDS {
         assertArrayEquals(new char[] {'a', 'b'}, pcds.getPasswordCharArray());
         final PrintWriter pw = new PrintWriter(System.err);
         pcds.setLogWriter(pw);
-        assertEquals(pw, pcds.getLogWriter());
+        @SuppressWarnings("resource")
+        final PrintWriter logWriter = pcds.getLogWriter();
+        assertEquals(pw, logWriter);
         pcds.setLoginTimeout(10);
         assertEquals(10, pcds.getLoginTimeout());
         pcds.setMaxIdle(100);
diff --git a/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java b/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java
index c14dba3..3fa89dd 100644
--- a/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java
+++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestCPDSConnectionFactory.java
@@ -108,7 +108,7 @@ public class TestCPDSConnectionFactory {
                 assertEquals(0, pool.getNumActive());
 
                 // Clear pool
-                factory.getPool().clear();
+                pool.clear();
                 assertEquals(0, pool.getNumIdle());
             }
         }
diff --git a/src/test/java/org/apache/commons/dbcp2/datasources/TestKeyedCPDSConnectionFactory.java b/src/test/java/org/apache/commons/dbcp2/datasources/TestKeyedCPDSConnectionFactory.java
index 62a58bf..e4a7fc8 100644
--- a/src/test/java/org/apache/commons/dbcp2/datasources/TestKeyedCPDSConnectionFactory.java
+++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestKeyedCPDSConnectionFactory.java
@@ -20,11 +20,12 @@ package org.apache.commons.dbcp2.datasources;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.junit.jupiter.api.Assertions.fail;
 
 import java.sql.Connection;
 import java.sql.SQLException;
+import java.time.Duration;
 
 import javax.sql.PooledConnection;
 
@@ -35,6 +36,7 @@ import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
 /**
+ * Tests {@link KeyedCPDSConnectionFactory}.
  */
 public class TestKeyedCPDSConnectionFactory {
 
@@ -60,70 +62,59 @@ public class TestKeyedCPDSConnectionFactory {
     public void testConnectionErrorCleanup() throws Exception {
         // Setup factory
         final UserPassKey key = new UserPassKey("userName", "password");
-        final KeyedCPDSConnectionFactory factory =
-            new KeyedCPDSConnectionFactory(cpds, null, -1, false);
-        final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool = new GenericKeyedObjectPool<>(factory);
-        factory.setPool(pool);
-
-        // Checkout a pair of connections
-        final PooledConnection pcon1 =
-            pool.borrowObject(key)
-                .getPooledConnection();
-        final Connection con1 = pcon1.getConnection();
-        final PooledConnection pcon2 =
-            pool.borrowObject(key)
-                .getPooledConnection();
-        assertEquals(2, pool.getNumActive(key));
-        assertEquals(0, pool.getNumIdle(key));
-
-        // Verify listening
-        final PooledConnectionProxy pc = (PooledConnectionProxy) pcon1;
-        assertTrue(pc.getListeners().contains(factory));
-
-        // Throw connectionError event
-        pc.throwConnectionError();
-
-        // Active count should be reduced by 1 and no idle increase
-        assertEquals(1, pool.getNumActive(key));
-        assertEquals(0, pool.getNumIdle(key));
-
-        // Throw another one - we should be on cleanup list, so ignored
-        pc.throwConnectionError();
-        assertEquals(1, pool.getNumActive(key));
-        assertEquals(0, pool.getNumIdle(key));
-
-        // Ask for another connection - should trigger makeObject, which causes
-        // cleanup, removing listeners.
-        final PooledConnection pcon3 =
-            pool.borrowObject(key)
-                .getPooledConnection();
-        assertNotEquals(pcon3, pcon1); // better not get baddie back
-        assertFalse(pc.getListeners().contains(factory)); // verify cleanup
-        assertEquals(2, pool.getNumActive(key));
-        assertEquals(0, pool.getNumIdle(key));
-
-        // Return good connections back to pool
-        pcon2.getConnection().close();
-        pcon3.getConnection().close();
-        assertEquals(2, pool.getNumIdle(key));
-        assertEquals(0, pool.getNumActive(key));
-
-        // Verify pc is closed
-        try {
-           pc.getConnection();
-           fail("Expecting SQLException using closed PooledConnection");
-        } catch (final SQLException ex) {
-            // expected
+        final KeyedCPDSConnectionFactory factory = new KeyedCPDSConnectionFactory(cpds, null, -1, false);
+        try (final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool = new GenericKeyedObjectPool<>(factory)) {
+            factory.setPool(pool);
+
+            // Checkout a pair of connections
+            final PooledConnection pcon1 = pool.borrowObject(key).getPooledConnection();
+            try (final Connection con1 = pcon1.getConnection()) {
+                final PooledConnection pcon2 = pool.borrowObject(key).getPooledConnection();
+                assertEquals(2, pool.getNumActive(key));
+                assertEquals(0, pool.getNumIdle(key));
+
+                // Verify listening
+                final PooledConnectionProxy pc = (PooledConnectionProxy) pcon1;
+                assertTrue(pc.getListeners().contains(factory));
+
+                // Throw connectionError event
+                pc.throwConnectionError();
+
+                // Active count should be reduced by 1 and no idle increase
+                assertEquals(1, pool.getNumActive(key));
+                assertEquals(0, pool.getNumIdle(key));
+
+                // Throw another one - we should be on cleanup list, so ignored
+                pc.throwConnectionError();
+                assertEquals(1, pool.getNumActive(key));
+                assertEquals(0, pool.getNumIdle(key));
+
+                // Ask for another connection - should trigger makeObject, which causes
+                // cleanup, removing listeners.
+                final PooledConnection pcon3 = pool.borrowObject(key).getPooledConnection();
+                assertNotEquals(pcon3, pcon1); // better not get baddie back
+                assertFalse(pc.getListeners().contains(factory)); // verify cleanup
+                assertEquals(2, pool.getNumActive(key));
+                assertEquals(0, pool.getNumIdle(key));
+
+                // Return good connections back to pool
+                pcon2.getConnection().close();
+                pcon3.getConnection().close();
+                assertEquals(2, pool.getNumIdle(key));
+                assertEquals(0, pool.getNumActive(key));
+
+                // Verify pc is closed
+                assertThrows(SQLException.class, () -> pc.getConnection(), "Expecting SQLException using closed PooledConnection");
+
+                // Back from the dead - ignore the ghost!
+            }
+            assertEquals(2, pool.getNumIdle(key));
+            assertEquals(0, pool.getNumActive(key));
+
+            // Clear pool
+            pool.clear();
+            assertEquals(0, pool.getNumIdle(key));
         }
-
-        // Back from the dead - ignore the ghost!
-        con1.close();
-        assertEquals(2, pool.getNumIdle(key));
-        assertEquals(0, pool.getNumActive(key));
-
-        // Clear pool
-        factory.getPool().clear();
-        assertEquals(0, pool.getNumIdle(key));
     }
 
     /**
@@ -132,14 +123,14 @@ public class TestKeyedCPDSConnectionFactory {
     @Test
     public void testNullValidationQuery() throws Exception {
         final UserPassKey key = new UserPassKey("userName", "password");
-        final KeyedCPDSConnectionFactory factory =
-                new KeyedCPDSConnectionFactory(cpds, null, -1, false);
-        final GenericKeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool = new GenericKeyedObjectPool<>(factory);
-        factory.setPool(pool);
-        pool.setTestOnBorrow(true);
-        final PooledConnection pcon = pool.borrowObject(key).getPooledConnection();
-        final Connection con = pcon.getConnection();
-        con.close();
+        final KeyedCPDSConnectionFactory factory = new KeyedCPDSConnectionFactory(cpds, null, -1, false);
+        try (final GenericKeyedObjectPool<UserPassKey, PooledConnectionAndInfo> pool = new GenericKeyedObjectPool<>(factory)) {
+            factory.setPool(pool);
+            pool.setTestOnBorrow(true);
+            final PooledConnection pcon = pool.borrowObject(key).getPooledConnection();
+            try (final Connection con = pcon.getConnection()) {
+            }
+        }
     }
 
     /**
@@ -151,21 +142,21 @@ public class TestKeyedCPDSConnectionFactory {
      */
     @Test
     public void testSharedPoolDSDestroyOnReturn() throws Exception {
-       final SharedPoolDataSource ds = new SharedPoolDataSource();
-       ds.setConnectionPoolDataSource(cpds);
-       ds.setMaxTotal(10);
-       ds.setDefaultMaxWaitMillis(50);
-       ds.setDefaultMaxIdle(2);
-       final Connection conn1 = ds.getConnection("userName", "password");
-       final Connection conn2 = ds.getConnection("userName", "password");
-       final Connection conn3 = ds.getConnection("userName", "password");
-       assertEquals(3, ds.getNumActive());
-       conn1.close();
-       assertEquals(1, ds.getNumIdle());
-       conn2.close();
-       assertEquals(2, ds.getNumIdle());
-       conn3.close(); // Return to pool will trigger destroy -> close sequence
-       assertEquals(2, ds.getNumIdle());
-       ds.close();
+        try (final SharedPoolDataSource ds = new SharedPoolDataSource()) {
+            ds.setConnectionPoolDataSource(cpds);
+            ds.setMaxTotal(10);
+            ds.setDefaultMaxWait(Duration.ofMillis(50));
+            ds.setDefaultMaxIdle(2);
+            final Connection conn1 = ds.getConnection("userName", "password");
+            final Connection conn2 = ds.getConnection("userName", "password");
+            final Connection conn3 = ds.getConnection("userName", "password");
+            assertEquals(3, ds.getNumActive());
+            conn1.close();
+            assertEquals(1, ds.getNumIdle());
+            conn2.close();
+            assertEquals(2, ds.getNumIdle());
+            conn3.close(); // Return to pool will trigger destroy -> close sequence
+            assertEquals(2, ds.getNumIdle());
+        }
     }
 }
diff --git a/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java b/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
index da81547..ff1f22e 100644
--- a/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
+++ b/src/test/java/org/apache/commons/dbcp2/datasources/TestSharedPoolDataSource.java
@@ -38,6 +38,7 @@ import org.apache.commons.dbcp2.DelegatingStatement;
 import org.apache.commons.dbcp2.TestConnectionPool;
 import org.apache.commons.dbcp2.TesterDriver;
 import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS;
+import org.apache.commons.lang3.ArrayUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
@@ -58,6 +59,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
             return conn.prepareCall("{call home()}", 0, 0);
         }
     }
+
     private static class CscbStringIntIntInt extends PrepareCallCallback {
         @Override
         CallableStatement getCallableStatement() throws SQLException {
@@ -69,24 +71,26 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
     // complexity to reduce what would otherwise be lots of copy and paste
     private static abstract class PrepareCallCallback {
         protected Connection conn;
+
         abstract CallableStatement getCallableStatement() throws SQLException;
+
         void setConnection(final Connection conn) {
             this.conn = conn;
         }
     }
 
-
     // There are 6 different prepareStatement statement methods so add a little
     // complexity to reduce what would otherwise be lots of copy and paste
     private static abstract class PrepareStatementCallback {
         protected Connection conn;
+
         abstract PreparedStatement getPreparedStatement() throws SQLException;
+
         void setConnection(final Connection conn) {
             this.conn = conn;
         }
     }
 
-
     private static class PscbString extends PrepareStatementCallback {
         @Override
         PreparedStatement getPreparedStatement() throws SQLException {
@@ -104,7 +108,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
     private static class PscbStringIntArray extends PrepareStatementCallback {
         @Override
         PreparedStatement getPreparedStatement() throws SQLException {
-            return conn.prepareStatement("select * from dual", new int[0]);
+            return conn.prepareStatement("select * from dual", ArrayUtils.EMPTY_INT_ARRAY);
         }
     }
 
@@ -125,7 +129,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
     private static class PscbStringStringArray extends PrepareStatementCallback {
         @Override
         PreparedStatement getPreparedStatement() throws SQLException {
-            return conn.prepareStatement("select * from dual", new String[0]);
+            return conn.prepareStatement("select * from dual", ArrayUtils.EMPTY_STRING_ARRAY);
         }
     }
 
@@ -134,7 +138,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
     private DataSource ds;
 
     private void doTestPoolCallableStatements(final PrepareCallCallback callBack)
-    throws Exception {
+        throws Exception {
         final DriverAdapterCPDS myPcds = new DriverAdapterCPDS();
         myPcds.setDriver("org.apache.commons.dbcp2.TesterDriver");
         myPcds.setUrl("jdbc:apache:commons:testdriver");
@@ -143,69 +147,69 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
         myPcds.setPoolPreparedStatements(true);
         myPcds.setMaxPreparedStatements(10);
 
-        final SharedPoolDataSource spDs = new SharedPoolDataSource();
-        spDs.setConnectionPoolDataSource(myPcds);
-        spDs.setMaxTotal(getMaxTotal());
-        spDs.setDefaultMaxWait(getMaxWaitDuration());
-        spDs.setDefaultTransactionIsolation(
-            Connection.TRANSACTION_READ_COMMITTED);
-
-        final DataSource myDs = spDs;
-
-        Connection conn = ds.getConnection();
-        callBack.setConnection(conn);
-
-        assertNotNull(conn);
-
-        CallableStatement stmt = callBack.getCallableStatement();
-        assertNotNull(stmt);
-        final long l1HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode();
-        ResultSet rset = stmt.executeQuery();
-        assertNotNull(rset);
-        assertTrue(rset.next());
-        rset.close();
-        stmt.close();
-
-        stmt = callBack.getCallableStatement();
-        assertNotNull(stmt);
-        final long l2HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode();
-        rset = stmt.executeQuery();
-        assertNotNull(rset);
-        assertTrue(rset.next());
-        rset.close();
-        stmt.close();
+        try (final SharedPoolDataSource spDs = new SharedPoolDataSource()) {
+            spDs.setConnectionPoolDataSource(myPcds);
+            spDs.setMaxTotal(getMaxTotal());
+            spDs.setDefaultMaxWait(getMaxWaitDuration());
+            spDs.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
+
+            final DataSource myDs = spDs;
+
+            try (Connection conn = ds.getConnection()) {
+                callBack.setConnection(conn);
+
+                assertNotNull(conn);
+                final long l1HashCode;
+                final long l2HashCode;
+                try (CallableStatement stmt = callBack.getCallableStatement()) {
+                    assertNotNull(stmt);
+                    l1HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode();
+                    try (ResultSet rset = stmt.executeQuery()) {
+                        assertNotNull(rset);
+                        assertTrue(rset.next());
+                    }
+                }
 
-        // statement pooling is not enabled, we should get different statements
-        assertTrue(l1HashCode != l2HashCode);
-        conn.close();
-        conn = null;
+                try (CallableStatement stmt = callBack.getCallableStatement()) {
+                    assertNotNull(stmt);
+                    l2HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode();
+                    try (ResultSet rset = stmt.executeQuery()) {
+                        assertNotNull(rset);
+                        assertTrue(rset.next());
+                    }
+                }
 
-        conn = myDs.getConnection();
-        callBack.setConnection(conn);
+                // statement pooling is not enabled, we should get different statements
+                assertTrue(l1HashCode != l2HashCode);
+            }
 
-        stmt = callBack.getCallableStatement();
-        assertNotNull(stmt);
-        final long l3HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode();
-        rset = stmt.executeQuery();
-        assertNotNull(rset);
-        assertTrue(rset.next());
-        rset.close();
-        stmt.close();
+            try (Connection conn = myDs.getConnection()) {
+                callBack.setConnection(conn);
+
+                final long l3HashCode;
+                final long l4HashCode;
+                try (CallableStatement stmt = callBack.getCallableStatement()) {
+                    assertNotNull(stmt);
+                    l3HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode();
+                    try (ResultSet rset = stmt.executeQuery()) {
+                        assertNotNull(rset);
+                        assertTrue(rset.next());
+                    }
+                }
 
-        stmt = callBack.getCallableStatement();
-        assertNotNull(stmt);
-        final long l4HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode();
-        rset = stmt.executeQuery();
-        assertNotNull(rset);
-        assertTrue(rset.next());
-        rset.close();
-        stmt.close();
+                try (CallableStatement stmt = callBack.getCallableStatement()) {
+                    assertNotNull(stmt);
+                    l4HashCode = ((DelegatingStatement) stmt).getDelegate().hashCode();
+                    try (ResultSet rset = stmt.executeQuery()) {
+                        assertNotNull(rset);
+                        assertTrue(rset.next());
+                    }
+                }
 
-        // prepared statement pooling is working
-        assertEquals(l3HashCode, l4HashCode);
-        conn.close();
-        conn = null;
-        spDs.close();
+                // prepared statement pooling is working
+                assertEquals(l3HashCode, l4HashCode);
+            }
+        }
     }
 
     private void doTestPoolPreparedStatements(final PrepareStatementCallback callBack)
@@ -419,8 +423,7 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
     public void testDbcp369() {
         final ArrayList<SharedPoolDataSource> dataSources = new ArrayList<>();
         for (int j = 0; j < 10000; j++) {
-            final SharedPoolDataSource dataSource = new SharedPoolDataSource();
-            dataSources.add(dataSource);
+            dataSources.add(new SharedPoolDataSource());
         }
 
         final Thread t1 = new Thread(() -> {
@@ -517,11 +520,11 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
         final int maxWaitMillis = 1000;
         final int theadCount = 20;
 
-        ((SharedPoolDataSource)ds).setDefaultMaxWaitMillis(maxWaitMillis);
+        ((SharedPoolDataSource) ds).setDefaultMaxWait(Duration.ofMillis(maxWaitMillis));
         // Obtain all the connections from the pool
         final Connection[] c = new Connection[getMaxTotal()];
-        for (int i=0; i<c.length; i++) {
-            c[i] = ds.getConnection("foo","bar");
+        for (int i = 0; i < c.length; i++) {
+            c[i] = ds.getConnection("foo", "bar");
             assertNotNull(c[i]);
         }
 
@@ -542,7 +545,6 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
             poolTest.getThread().join();
         }
 
-
         final long endMillis = System.currentTimeMillis();
 
         // System.out.println("testMaxWaitMillis took " + (end - start) + " ms. maxWaitMillis: " + maxWaitMillis);
@@ -663,14 +665,11 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
 
     @Override
     @Test
-    public void testSimple2()
-        throws Exception
-    {
+    public void testSimple2() throws Exception {
         Connection conn = ds.getConnection();
         assertNotNull(conn);
 
-        PreparedStatement stmt =
-            conn.prepareStatement("select * from dual");
+        PreparedStatement stmt = conn.prepareStatement("select * from dual");
         assertNotNull(stmt);
         ResultSet rset = stmt.executeQuery();
         assertNotNull(rset);
@@ -687,9 +686,9 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
         stmt.close();
 
         conn.close();
-        try (Statement s = conn.createStatement()){
+        try (Statement s = conn.createStatement()) {
             fail("Can't use closed connections");
-        } catch(final SQLException e) {
+        } catch (final SQLException e) {
             // expected
         }
 
@@ -713,40 +712,35 @@ public class TestSharedPoolDataSource extends TestConnectionPool {
         stmt.close();
 
         conn.close();
-        conn = null;
     }
 
     @Test
-    public void testSimpleWithUsername() throws Exception
-    {
-        final Connection conn = ds.getConnection("u1", "p1");
-        assertNotNull(conn);
-        final PreparedStatement stmt = conn.prepareStatement("select * from dual");
-        assertNotNull(stmt);
-        final ResultSet rset = stmt.executeQuery();
-        assertNotNull(rset);
-        assertTrue(rset.next());
-        rset.close();
-        stmt.close();
-        conn.close();
+    public void testSimpleWithUsername() throws Exception {
+        try (final Connection conn = ds.getConnection("u1", "p1")) {
+            assertNotNull(conn);
+            try (final PreparedStatement stmt = conn.prepareStatement("select * from dual")) {
+                assertNotNull(stmt);
+                try (final ResultSet rset = stmt.executeQuery()) {
+                    assertNotNull(rset);
+                    assertTrue(rset.next());
+                }
+            }
+        }
     }
 
     @Test
     public void testTransactionIsolationBehavior() throws Exception {
-        final Connection conn = getConnection();
-        assertNotNull(conn);
-        assertEquals(Connection.TRANSACTION_READ_COMMITTED,
-                     conn.getTransactionIsolation());
-        conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
-        conn.close();
+        try (final Connection conn = getConnection()) {
+            assertNotNull(conn);
+            assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn.getTransactionIsolation());
+            conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
+        }
 
         final Connection conn2 = getConnection();
-        assertEquals(Connection.TRANSACTION_READ_COMMITTED,
-                     conn2.getTransactionIsolation());
+        assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn2.getTransactionIsolation());
 
         final Connection conn3 = getConnection();
-        assertEquals(Connection.TRANSACTION_READ_COMMITTED,
-                     conn3.getTransactionIsolation());
+        assertEquals(Connection.TRANSACTION_READ_COMMITTED, conn3.getTransactionIsolation());
         conn2.close();
         conn3.close();
     }