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 2023/08/27 20:17:08 UTC

[commons-dbcp] branch master updated (3e9e1b70 -> 2a10ef8a)

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

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git


    from 3e9e1b70 Use assertThrows()
     new 83e7cf68 Use try-with-resources
     new 2a10ef8a Use try-with-resources

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../apache/commons/dbcp2/TestConnectionPool.java   | 314 +++++++++++----------
 1 file changed, 160 insertions(+), 154 deletions(-)


[commons-dbcp] 01/02: Use try-with-resources

Posted by gg...@apache.org.
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

commit 83e7cf6850a0d0c010bfbd6cc4529af869d89c4f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 27 15:58:08 2023 -0400

    Use try-with-resources
---
 .../apache/commons/dbcp2/TestConnectionPool.java   | 28 +++++++++++-----------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java b/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
index ab1b66b1..3f1c78d3 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
@@ -916,22 +916,22 @@ public abstract class TestConnectionPool {
         try (Connection conn2 = newConnection()) {
             assertNotNull(conn2);
             {
-                final PreparedStatement stmt = conn2.prepareStatement("select * from dual");
-                assertNotNull(stmt);
-                final ResultSet rset = stmt.executeQuery();
-                assertNotNull(rset);
-                assertTrue(rset.next());
-                rset.close();
-                stmt.close();
+                try (PreparedStatement stmt = conn2.prepareStatement("select * from dual")) {
+                    assertNotNull(stmt);
+                    try (ResultSet rset = stmt.executeQuery()) {
+                        assertNotNull(rset);
+                        assertTrue(rset.next());
+                    }
+                }
             }
             {
-                final PreparedStatement stmt = conn2.prepareStatement("select * from dual");
-                assertNotNull(stmt);
-                final ResultSet rset = stmt.executeQuery();
-                assertNotNull(rset);
-                assertTrue(rset.next());
-                rset.close();
-                stmt.close();
+                try (PreparedStatement stmt = conn2.prepareStatement("select * from dual")) {
+                    assertNotNull(stmt);
+                    try (ResultSet rset = stmt.executeQuery()) {
+                        assertNotNull(rset);
+                        assertTrue(rset.next());
+                    }
+                }
             }
         }
     }


[commons-dbcp] 02/02: Use try-with-resources

Posted by gg...@apache.org.
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

commit 2a10ef8a39790295375454d2c927c770e4937faf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 27 16:17:03 2023 -0400

    Use try-with-resources
---
 .../apache/commons/dbcp2/TestConnectionPool.java   | 286 +++++++++++----------
 1 file changed, 146 insertions(+), 140 deletions(-)

diff --git a/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java b/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
index 3f1c78d3..ec680c24 100644
--- a/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
+++ b/src/test/java/org/apache/commons/dbcp2/TestConnectionPool.java
@@ -120,29 +120,28 @@ public abstract class TestConnectionPool {
                     loops++;
                     state = "Getting Connection";
                     preconnected = timeStampMillis();
-                    final Connection conn = getConnection();
-                    connHash = System.identityHashCode(((DelegatingConnection<?>)conn).getInnermostDelegate());
-                    connected = timeStampMillis();
-                    state = "Using Connection";
-                    assertNotNull(conn);
-                    final String sql = numStatements == 1 ? "select * from dual" : "select count " + random.nextInt(numStatements - 1);
-                    final PreparedStatement stmt =
-                        conn.prepareStatement(sql);
-                    assertNotNull(stmt);
-                    final ResultSet rset = stmt.executeQuery();
-                    assertNotNull(rset);
-                    assertTrue(rset.next());
-                    state = "Holding Connection";
-                    Thread.sleep(connHoldDuration.toMillis());
-                    state = "Closing ResultSet";
-                    rset.close();
-                    state = "Closing Statement";
-                    stmt.close();
-                    state = "Closing Connection";
-                    conn.close();
+                    try (Connection conn = getConnection()) {
+                        connHash = System.identityHashCode(((DelegatingConnection<?>) conn).getInnermostDelegate());
+                        connected = timeStampMillis();
+                        state = "Using Connection";
+                        assertNotNull(conn);
+                        final String sql = numStatements == 1 ? "select * from dual" : "select count " + random.nextInt(numStatements - 1);
+                        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
+                            assertNotNull(stmt);
+                            try (ResultSet rset = stmt.executeQuery()) {
+                                assertNotNull(rset);
+                                assertTrue(rset.next());
+                                state = "Holding Connection";
+                                Thread.sleep(connHoldDuration.toMillis());
+                                state = "Closing ResultSet";
+                            }
+                            state = "Closing Statement";
+                        }
+                        state = "Closing Connection";
+                    }
                     postconnected = timeStampMillis();
                     state = "Closed";
-                    if (loopOnce){
+                    if (loopOnce) {
                         break; // Or could set isRun=false
                     }
                 }
@@ -195,19 +194,18 @@ public abstract class TestConnectionPool {
 
         @Override
         public void run() {
-            for(int i=0;i<_iter;i++) {
+            for (int i = 0; i < _iter; i++) {
                 try {
                     Thread.sleep(_random.nextInt(_delay));
                 } catch (final Exception e) {
                     // ignored
                 }
                 try (Connection conn = newConnection();
-                        PreparedStatement stmt = conn.prepareStatement(
-                                "select 'literal', SYSDATE from dual");
+                        PreparedStatement stmt = conn.prepareStatement("select 'literal', SYSDATE from dual");
                         ResultSet rset = stmt.executeQuery()) {
                     try {
                         Thread.sleep(_random.nextInt(_delay));
-                    } catch (final Exception e) {
+                    } catch (final Exception ignore) {
                         // ignored
                     }
                 } catch (final Exception e) {
@@ -463,7 +461,13 @@ public abstract class TestConnectionPool {
         }
     }
 
-    /** Acquire a connection and push it onto the connections stack */
+    /** 
+     * Acquires a new connection and push it onto the connections stack.
+     * 
+     * @return a new connection.
+     * @throws Exception Defined in subclasses.
+     */
+    @SuppressWarnings("resource") // Caller closes
     protected Connection newConnection() throws Exception {
         return connectionStack.push(getConnection());
     }
@@ -543,21 +547,21 @@ public abstract class TestConnectionPool {
 
     @Test
     public void testCanCloseCallableStatementTwice() throws Exception {
-        final Connection conn = newConnection();
-        assertNotNull(conn);
-        assertFalse(conn.isClosed());
-        for(int i=0;i<2;i++) { // loop to show we *can* close again once we've borrowed it from the pool again
-            final PreparedStatement stmt = conn.prepareCall("select * from dual");
-            assertNotNull(stmt);
-            assertFalse(isClosed(stmt));
-            stmt.close();
-            assertTrue(isClosed(stmt));
-            stmt.close();
-            assertTrue(isClosed(stmt));
-            stmt.close();
-            assertTrue(isClosed(stmt));
+        try (Connection conn = newConnection()) {
+            assertNotNull(conn);
+            assertFalse(conn.isClosed());
+            for (int i = 0; i < 2; i++) { // loop to show we *can* close again once we've borrowed it from the pool again
+                final PreparedStatement stmt = conn.prepareCall("select * from dual");
+                assertNotNull(stmt);
+                assertFalse(isClosed(stmt));
+                stmt.close();
+                assertTrue(isClosed(stmt));
+                stmt.close();
+                assertTrue(isClosed(stmt));
+                stmt.close();
+                assertTrue(isClosed(stmt));
+            }
         }
-        conn.close();
     }
 
     /**
@@ -579,42 +583,42 @@ public abstract class TestConnectionPool {
 
     @Test
     public void testCanClosePreparedStatementTwice() throws Exception {
-        final Connection conn = newConnection();
-        assertNotNull(conn);
-        assertFalse(conn.isClosed());
-        for(int i=0;i<2;i++) { // loop to show we *can* close again once we've borrowed it from the pool again
-            final PreparedStatement stmt = conn.prepareStatement("select * from dual");
-            assertNotNull(stmt);
-            assertFalse(isClosed(stmt));
-            stmt.close();
-            assertTrue(isClosed(stmt));
-            stmt.close();
-            assertTrue(isClosed(stmt));
-            stmt.close();
-            assertTrue(isClosed(stmt));
+        try (Connection conn = newConnection()) {
+            assertNotNull(conn);
+            assertFalse(conn.isClosed());
+            for (int i = 0; i < 2; i++) { // loop to show we *can* close again once we've borrowed it from the pool again
+                final PreparedStatement stmt = conn.prepareStatement("select * from dual");
+                assertNotNull(stmt);
+                assertFalse(isClosed(stmt));
+                stmt.close();
+                assertTrue(isClosed(stmt));
+                stmt.close();
+                assertTrue(isClosed(stmt));
+                stmt.close();
+                assertTrue(isClosed(stmt));
+            }
         }
-        conn.close();
     }
 
     @Test
     public void testCanCloseResultSetTwice() throws Exception {
-        final Connection conn = newConnection();
-        assertNotNull(conn);
-        assertFalse(conn.isClosed());
-        for(int i=0;i<2;i++) { // loop to show we *can* close again once we've borrowed it from the pool again
-            final PreparedStatement stmt = conn.prepareStatement("select * from dual");
-            assertNotNull(stmt);
-            final ResultSet rset = stmt.executeQuery();
-            assertNotNull(rset);
-            assertFalse(isClosed(rset));
-            rset.close();
-            assertTrue(isClosed(rset));
-            rset.close();
-            assertTrue(isClosed(rset));
-            rset.close();
-            assertTrue(isClosed(rset));
+        try (Connection conn = newConnection()) {
+            assertNotNull(conn);
+            assertFalse(conn.isClosed());
+            for (int i = 0; i < 2; i++) { // loop to show we *can* close again once we've borrowed it from the pool again
+                final PreparedStatement stmt = conn.prepareStatement("select * from dual");
+                assertNotNull(stmt);
+                final ResultSet rset = stmt.executeQuery();
+                assertNotNull(rset);
+                assertFalse(isClosed(rset));
+                rset.close();
+                assertTrue(isClosed(rset));
+                rset.close();
+                assertTrue(isClosed(rset));
+                rset.close();
+                assertTrue(isClosed(rset));
+            }
         }
-        conn.close();
     }
 
     @Test
@@ -622,7 +626,7 @@ public abstract class TestConnectionPool {
         final Connection conn = newConnection();
         assertNotNull(conn);
         assertFalse(conn.isClosed());
-        for(int i=0;i<2;i++) { // loop to show we *can* close again once we've borrowed it from the pool again
+        for (int i = 0; i < 2; i++) { // loop to show we *can* close again once we've borrowed it from the pool again
             final Statement stmt = conn.createStatement();
             assertNotNull(stmt);
             assertFalse(isClosed(stmt));
@@ -644,7 +648,8 @@ public abstract class TestConnectionPool {
             assertNotNull(c[i]);
 
             // generate SQLWarning on connection
-            try (CallableStatement cs = c[i].prepareCall("warning")){
+            try (CallableStatement cs = c[i].prepareCall("warning")) {
+                // empty
             }
         }
 
@@ -735,18 +740,22 @@ public abstract class TestConnectionPool {
 
     @Test
     public void testIsClosed() throws Exception {
-        for(int i=0;i<getMaxTotal();i++) {
+        for (int i = 0; i < getMaxTotal(); i++) {
+            @SuppressWarnings("resource")
             final Connection conn = newConnection();
-            assertNotNull(conn);
-            assertFalse(conn.isClosed());
-            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();
+            try {
+                assertNotNull(conn);
+                assertFalse(conn.isClosed());
+                try (PreparedStatement stmt = conn.prepareStatement("select * from dual")) {
+                    assertNotNull(stmt);
+                    try (ResultSet rset = stmt.executeQuery()) {
+                        assertNotNull(rset);
+                        assertTrue(rset.next());
+                    }
+                }
+            } finally {
+                conn.close();
+            }
             assertTrue(conn.isClosed());
         }
     }
@@ -772,15 +781,15 @@ public abstract class TestConnectionPool {
     // wrong order of passivate/close when a rset isn't closed
     @Test
     public void testNoRsetClose() throws Exception {
-        final Connection conn = newConnection();
-        assertNotNull(conn);
-        final PreparedStatement stmt = conn.prepareStatement("test");
-        assertNotNull(stmt);
-        final ResultSet rset = stmt.getResultSet();
-        assertNotNull(rset);
-        // forget to close the resultset: rset.close();
-        stmt.close();
-        conn.close();
+        try (Connection conn = newConnection()) {
+            assertNotNull(conn);
+            try (PreparedStatement stmt = conn.prepareStatement("test")) {
+                assertNotNull(stmt);
+                final ResultSet rset = stmt.getResultSet();
+                assertNotNull(rset);
+                // forget to close the resultset: rset.close();
+            }
+        }
     }
 
     @Test
@@ -819,19 +828,18 @@ public abstract class TestConnectionPool {
         // the new ones come from the pool
         for (final Connection element : c) {
             element.close();
-            final Connection con = newConnection();
-            final Connection underCon =
-                ((DelegatingConnection<?>) con).getInnermostDelegate();
-            assertNotNull(underCon, "Failed to get connection");
-            boolean found = false;
-            for (int j = 0; j < c.length; j++) {
-                if (underCon == u[j]) {
-                    found = true;
-                    break;
+            try (Connection con = newConnection()) {
+                final Connection underCon = ((DelegatingConnection<?>) con).getInnermostDelegate();
+                assertNotNull(underCon, "Failed to get connection");
+                boolean found = false;
+                for (int j = 0; j < c.length; j++) {
+                    if (underCon == u[j]) {
+                        found = true;
+                        break;
+                    }
                 }
+                assertTrue(found, "New connection not from pool");
             }
-            assertTrue(found, "New connection not from pool");
-            con.close();
         }
     }
 
@@ -839,53 +847,51 @@ public abstract class TestConnectionPool {
     // and Concurrency if statement pooling is not enabled
     // https://issues.apache.org/bugzilla/show_bug.cgi?id=24328
     @Test
-    public void testPrepareStatementOptions() throws Exception
-    {
-        final Connection conn = newConnection();
-        assertNotNull(conn);
-        final PreparedStatement stmt = conn.prepareStatement("select * from dual",
-            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
-        assertNotNull(stmt);
-        final ResultSet rset = stmt.executeQuery();
-        assertNotNull(rset);
-        assertTrue(rset.next());
-
-        assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, rset.getType());
-        assertEquals(ResultSet.CONCUR_UPDATABLE, rset.getConcurrency());
-
-        rset.close();
-        stmt.close();
-        conn.close();
+    public void testPrepareStatementOptions() throws Exception {
+        try (Connection conn = newConnection()) {
+            assertNotNull(conn);
+            try (PreparedStatement stmt = conn.prepareStatement("select * from dual", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE)) {
+                assertNotNull(stmt);
+                try (ResultSet rset = stmt.executeQuery()) {
+                    assertNotNull(rset);
+                    assertTrue(rset.next());
+
+                    assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, rset.getType());
+                    assertEquals(ResultSet.CONCUR_UPDATABLE, rset.getConcurrency());
+
+                }
+            }
+        }
     }
 
     @Test
     public void testRepeatedBorrowAndReturn() throws Exception {
         for (int i = 0; i < 100; i++) {
-            final Connection conn = newConnection();
-            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();
+            try (Connection conn = newConnection()) {
+                assertNotNull(conn);
+                try (PreparedStatement stmt = conn.prepareStatement("select * from dual")) {
+                    assertNotNull(stmt);
+                    try (ResultSet rset = stmt.executeQuery()) {
+                        assertNotNull(rset);
+                        assertTrue(rset.next());
+                    }
+                }
+            }
         }
     }
 
     @Test
     public void testSimple() throws Exception {
-        final Connection conn = newConnection();
-        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();
+        try (Connection conn = newConnection()) {
+            assertNotNull(conn);
+            try (PreparedStatement stmt = conn.prepareStatement("select * from dual")) {
+                assertNotNull(stmt);
+                try (ResultSet rset = stmt.executeQuery()) {
+                    assertNotNull(rset);
+                    assertTrue(rset.next());
+                }
+            }
+        }
     }
 
     @Test