You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/03/12 14:12:53 UTC

[shardingsphere] branch master updated: Close connections with try-with-resource in transaction test cases (#24564)

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

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 9ab1721f43c Close connections with try-with-resource in transaction test cases (#24564)
9ab1721f43c is described below

commit 9ab1721f43c032ac1afd7db1a0110791454cdb29
Author: ZhangCheng <fl...@outlook.com>
AuthorDate: Sun Mar 12 22:12:45 2023 +0800

    Close connections with try-with-resource in transaction test cases (#24564)
    
    * Close connections with try-with-resource in transaction test cases
    
    * Close connections with try-with-resource in transaction test cases
    
    * Close connections with try-with-resource in transaction test cases
---
 .../cases/alterresource/AddResourceTestCase.java   | 44 ++++++-------
 .../cases/alterresource/CloseResourceTestCase.java | 42 +++++++------
 .../cases/autocommit/MySQLAutoCommitTestCase.java  | 24 ++++----
 .../autocommit/PostgreSQLAutoCommitTestCase.java   | 24 ++++----
 .../cases/base/BaseTransactionTestCase.java        | 14 ++---
 .../classictransfer/ClassicTransferTestCase.java   |  5 +-
 .../BroadcastTableTransactionTestCase.java         | 37 ++++++-----
 .../ExceptionInTransactionTestCase.java            |  5 +-
 .../MultiTableCommitAndRollbackTestCase.java       | 58 ++++++++---------
 .../SingleTableCommitAndRollbackTestCase.java      | 34 +++++-----
 .../cases/cursor/OpenGaussCursorTestCase.java      | 22 ++++---
 .../cases/nested/NestedTransactionTestCase.java    | 26 ++++----
 .../cases/readonly/MySQLSetReadOnlyTestCase.java   | 12 ++--
 .../readonly/PostgreSQLSetReadOnlyTestCase.java    | 12 ++--
 .../cases/readonly/SetReadOnlyTestCase.java        | 15 ++---
 .../cases/savepoint/BaseSavePointTestCase.java     | 50 +++++++--------
 .../savepoint/OpenGaussSavePointTestCase.java      | 37 +++++------
 .../savepoint/PostgreSQLSavePointTestCase.java     | 41 ++++++------
 .../cases/truncate/MySQLLocalTruncateTestCase.java | 42 ++++++-------
 .../cases/truncate/MySQLXATruncateTestCase.java    | 34 +++++-----
 .../PostgreSQLAndOpenGaussTruncateTestCase.java    | 46 +++++++-------
 .../engine/base/TransactionBaseE2EIT.java          | 72 ++++++++++++----------
 .../engine/base/TransactionContainerComposer.java  |  2 +-
 23 files changed, 361 insertions(+), 337 deletions(-)

diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/AddResourceTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/AddResourceTestCase.java
index 81d7238c283..ec2914ae9ad 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/AddResourceTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/AddResourceTestCase.java
@@ -46,13 +46,13 @@ public final class AddResourceTestCase extends BaseTransactionTestCase {
     }
     
     private void assertAddResource(final TransactionContainerComposer containerComposer) throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        getBaseTransactionITCase().addResource(connection, "transaction_it_2", containerComposer);
-        createThreeDataSourceAccountTableRule(connection);
-        reCreateAccountTable(connection);
-        assertRollback();
-        assertCommit();
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            getBaseTransactionITCase().addResource(connection, "transaction_it_2", containerComposer);
+            createThreeDataSourceAccountTableRule(connection);
+            reCreateAccountTable(connection);
+            assertRollback();
+            assertCommit();
+        }
     }
     
     private void createThreeDataSourceAccountTableRule(final Connection connection) throws SQLException {
@@ -68,22 +68,24 @@ public final class AddResourceTestCase extends BaseTransactionTestCase {
     }
     
     private void assertRollback() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
-        executeWithLog(connection, "insert into account(id, BALANCE, TRANSACTION_ID) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6);");
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
-        connection.rollback();
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
+            executeWithLog(connection, "insert into account(id, BALANCE, TRANSACTION_ID) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6);");
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
+            connection.rollback();
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
+        }
     }
     
     private void assertCommit() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
-        executeWithLog(connection, "insert into account(id, BALANCE, TRANSACTION_ID) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6);");
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
-        connection.commit();
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
+            executeWithLog(connection, "insert into account(id, BALANCE, TRANSACTION_ID) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6);");
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
+            connection.commit();
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/CloseResourceTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/CloseResourceTestCase.java
index f7d6ae0439c..1413c1212c7 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/CloseResourceTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/alterresource/CloseResourceTestCase.java
@@ -45,12 +45,12 @@ public final class CloseResourceTestCase extends BaseTransactionTestCase {
     }
     
     private void assertCloseResource(final TransactionContainerComposer containerComposer) throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        getBaseTransactionITCase().createOriginalAccountTableRule(connection, containerComposer);
-        reCreateAccountTable(connection);
-        assertRollback();
-        assertCommit();
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            getBaseTransactionITCase().createOriginalAccountTableRule(connection, containerComposer);
+            reCreateAccountTable(connection);
+            assertRollback();
+            assertCommit();
+        }
     }
     
     private void reCreateAccountTable(final Connection connection) throws SQLException {
@@ -59,22 +59,24 @@ public final class CloseResourceTestCase extends BaseTransactionTestCase {
     }
     
     private void assertRollback() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
-        executeWithLog(connection, "insert into account(id, BALANCE, TRANSACTION_ID) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6);");
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
-        connection.rollback();
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
+            executeWithLog(connection, "insert into account(id, BALANCE, TRANSACTION_ID) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6);");
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
+            connection.rollback();
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
+        }
     }
     
     private void assertCommit() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
-        executeWithLog(connection, "insert into account(id, BALANCE, TRANSACTION_ID) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6);");
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
-        connection.commit();
-        assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 0);
+            executeWithLog(connection, "insert into account(id, BALANCE, TRANSACTION_ID) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6);");
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
+            connection.commit();
+            assertTableRowCount(connection, TransactionTestConstants.ACCOUNT, 6);
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
index 5b06887e6f0..c2964a5cb58 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/MySQLAutoCommitTestCase.java
@@ -50,18 +50,16 @@ public final class MySQLAutoCommitTestCase extends BaseTransactionTestCase {
     
     private void assertAutoCommit() throws SQLException {
         // TODO Currently XA transaction does not support two transactions in the same thread at the same time
-        Connection connection1 = getDataSource().getConnection();
-        Connection connection2 = getDataSource().getConnection();
-        executeWithLog(connection1, "set session transaction isolation level read committed;");
-        executeWithLog(connection2, "set session transaction isolation level read committed;");
-        connection1.setAutoCommit(false);
-        connection2.setAutoCommit(false);
-        executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1);");
-        assertFalse(executeQueryWithLog(connection2, "select * from account;").next());
-        connection1.commit();
-        ThreadUtil.sleep(1, TimeUnit.SECONDS);
-        assertTrue(executeQueryWithLog(connection2, "select * from account;").next());
-        connection1.close();
-        connection2.close();
+        try (Connection connection1 = getDataSource().getConnection(); Connection connection2 = getDataSource().getConnection()) {
+            executeWithLog(connection1, "set session transaction isolation level read committed;");
+            executeWithLog(connection2, "set session transaction isolation level read committed;");
+            connection1.setAutoCommit(false);
+            connection2.setAutoCommit(false);
+            executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1);");
+            assertFalse(executeQueryWithLog(connection2, "select * from account;").next());
+            connection1.commit();
+            ThreadUtil.sleep(1, TimeUnit.SECONDS);
+            assertTrue(executeQueryWithLog(connection2, "select * from account;").next());
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgreSQLAutoCommitTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgreSQLAutoCommitTestCase.java
index 961a088e762..c94a8171ed4 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgreSQLAutoCommitTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/autocommit/PostgreSQLAutoCommitTestCase.java
@@ -49,18 +49,16 @@ public final class PostgreSQLAutoCommitTestCase extends BaseTransactionTestCase
     }
     
     private void assertAutoCommit() throws SQLException {
-        Connection connection1 = getDataSource().getConnection();
-        Connection connection2 = getDataSource().getConnection();
-        executeWithLog(connection1, "set transaction isolation level read committed;");
-        executeWithLog(connection2, "set transaction isolation level read committed;");
-        connection1.setAutoCommit(false);
-        connection2.setAutoCommit(false);
-        executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1);");
-        assertFalse(executeQueryWithLog(connection2, "select * from account;").next());
-        connection1.commit();
-        ThreadUtil.sleep(1, TimeUnit.SECONDS);
-        assertTrue(executeQueryWithLog(connection2, "select * from account;").next());
-        connection1.close();
-        connection2.close();
+        try (Connection connection1 = getDataSource().getConnection(); Connection connection2 = getDataSource().getConnection()) {
+            executeWithLog(connection1, "set transaction isolation level read committed;");
+            executeWithLog(connection2, "set transaction isolation level read committed;");
+            connection1.setAutoCommit(false);
+            connection2.setAutoCommit(false);
+            executeWithLog(connection1, "insert into account(id, balance, transaction_id) values(1, 100, 1);");
+            assertFalse(executeQueryWithLog(connection2, "select * from account;").next());
+            connection1.commit();
+            ThreadUtil.sleep(1, TimeUnit.SECONDS);
+            assertTrue(executeQueryWithLog(connection2, "select * from account;").next());
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/base/BaseTransactionTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/base/BaseTransactionTestCase.java
index b88a99a19d6..f793b701996 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/base/BaseTransactionTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/base/BaseTransactionTestCase.java
@@ -61,13 +61,13 @@ public abstract class BaseTransactionTestCase {
     protected abstract void executeTest(TransactionContainerComposer containerComposer) throws SQLException;
     
     protected void beforeTest() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        executeWithLog(connection, "delete from account;");
-        executeWithLog(connection, "delete from t_order;");
-        executeWithLog(connection, "delete from t_order_item;");
-        connection.commit();
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            executeWithLog(connection, "delete from account;");
+            executeWithLog(connection, "delete from t_order;");
+            executeWithLog(connection, "delete from t_order_item;");
+            connection.commit();
+        }
     }
     
     protected void afterTest() throws SQLException {
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/classictransfer/ClassicTransferTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/classictransfer/ClassicTransferTestCase.java
index 1650ca3c1e9..5a7a96702d7 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/classictransfer/ClassicTransferTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/classictransfer/ClassicTransferTestCase.java
@@ -48,8 +48,9 @@ public final class ClassicTransferTestCase extends BaseTransactionTestCase {
     
     @Override
     public void executeTest(final TransactionContainerComposer containerComposer) throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        executeUpdateWithLog(connection, "insert into account(transaction_id, balance) values (1,0), (2,100);");
+        try (Connection connection = getDataSource().getConnection()) {
+            executeUpdateWithLog(connection, "insert into account(transaction_id, balance) values (1,0), (2,100);");
+        }
         innerRun();
     }
     
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/BroadcastTableTransactionTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/BroadcastTableTransactionTestCase.java
index 4e2beefb701..4f752361667 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/BroadcastTableTransactionTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/BroadcastTableTransactionTestCase.java
@@ -59,28 +59,31 @@ public final class BroadcastTableTransactionTestCase extends BaseTransactionTest
     }
     
     private void init() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        executeWithLog(connection, "delete from t_address;");
-        assertTableRowCount(connection, T_ADDRESS, 0);
+        try (Connection connection = getDataSource().getConnection()) {
+            executeWithLog(connection, "delete from t_address;");
+            assertTableRowCount(connection, T_ADDRESS, 0);
+        }
     }
     
     private void commit() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        executeWithLog(connection, "delete from t_address;");
-        assertTableRowCount(connection, T_ADDRESS, 0);
-        executeWithLog(connection, "INSERT INTO t_address (id, code, address) VALUES (1, '1', 'nanjing');");
-        assertTableRowCount(connection, T_ADDRESS, 1);
-        connection.commit();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            executeWithLog(connection, "delete from t_address;");
+            assertTableRowCount(connection, T_ADDRESS, 0);
+            executeWithLog(connection, "INSERT INTO t_address (id, code, address) VALUES (1, '1', 'nanjing');");
+            assertTableRowCount(connection, T_ADDRESS, 1);
+            connection.commit();
+        }
     }
     
     private void rollback() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        executeWithLog(connection, "delete from t_address;");
-        assertTableRowCount(connection, T_ADDRESS, 0);
-        executeWithLog(connection, "INSERT INTO t_address (id, code, address) VALUES (1, '1', 'nanjing');");
-        assertTableRowCount(connection, T_ADDRESS, 1);
-        connection.commit();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            executeWithLog(connection, "delete from t_address;");
+            assertTableRowCount(connection, T_ADDRESS, 0);
+            executeWithLog(connection, "INSERT INTO t_address (id, code, address) VALUES (1, '1', 'nanjing');");
+            assertTableRowCount(connection, T_ADDRESS, 1);
+            connection.commit();
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/ExceptionInTransactionTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/ExceptionInTransactionTestCase.java
index 96f378fbbd2..731ccf778f0 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/ExceptionInTransactionTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/ExceptionInTransactionTestCase.java
@@ -58,10 +58,13 @@ public final class ExceptionInTransactionTestCase extends BaseTransactionTestCas
             fail("It should fail here.");
         } catch (final ArithmeticException ex) {
             assertThat(ex.getMessage(), is("/ by zero"));
-        } finally {
             if (null != connection) {
                 connection.rollback();
             }
+        } finally {
+            if (null != connection) {
+                connection.close();
+            }
         }
         Thread queryThread = new Thread(() -> {
             try (Connection connection2 = getDataSource().getConnection()) {
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/MultiTableCommitAndRollbackTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/MultiTableCommitAndRollbackTestCase.java
index 170e64a42e3..df594e3e934 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/MultiTableCommitAndRollbackTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/MultiTableCommitAndRollbackTestCase.java
@@ -47,36 +47,38 @@ public final class MultiTableCommitAndRollbackTestCase extends BaseTransactionTe
     }
     
     private void assertRollback() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertTableRowCount(connection, T_ORDER, 0);
-        assertTableRowCount(connection, T_ORDER_ITEM, 0);
-        executeSqlListWithLog(connection,
-                "INSERT INTO t_order (order_id, user_id, status) VALUES (1, 1, '1');",
-                "INSERT INTO t_order (order_id, user_id, status) VALUES (2, 2, '2');",
-                "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (1, 1, 1, '1');",
-                "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (2, 2, 2, '2');");
-        assertTableRowCount(connection, T_ORDER, 2);
-        assertTableRowCount(connection, T_ORDER_ITEM, 2);
-        connection.rollback();
-        assertTableRowCount(connection, T_ORDER, 0);
-        assertTableRowCount(connection, T_ORDER_ITEM, 0);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertTableRowCount(connection, T_ORDER, 0);
+            assertTableRowCount(connection, T_ORDER_ITEM, 0);
+            executeSqlListWithLog(connection,
+                    "INSERT INTO t_order (order_id, user_id, status) VALUES (1, 1, '1');",
+                    "INSERT INTO t_order (order_id, user_id, status) VALUES (2, 2, '2');",
+                    "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (1, 1, 1, '1');",
+                    "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (2, 2, 2, '2');");
+            assertTableRowCount(connection, T_ORDER, 2);
+            assertTableRowCount(connection, T_ORDER_ITEM, 2);
+            connection.rollback();
+            assertTableRowCount(connection, T_ORDER, 0);
+            assertTableRowCount(connection, T_ORDER_ITEM, 0);
+        }
     }
     
     private void assertCommit() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertTableRowCount(connection, T_ORDER, 0);
-        assertTableRowCount(connection, T_ORDER_ITEM, 0);
-        executeSqlListWithLog(connection,
-                "INSERT INTO t_order (order_id, user_id, status) VALUES (1, 1, '1');",
-                "INSERT INTO t_order (order_id, user_id, status) VALUES (2, 2, '2');",
-                "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (1, 1, 1, '1');",
-                "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (2, 2, 2, '2');");
-        assertTableRowCount(connection, T_ORDER, 2);
-        assertTableRowCount(connection, T_ORDER_ITEM, 2);
-        connection.commit();
-        assertTableRowCount(connection, T_ORDER, 2);
-        assertTableRowCount(connection, T_ORDER_ITEM, 2);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertTableRowCount(connection, T_ORDER, 0);
+            assertTableRowCount(connection, T_ORDER_ITEM, 0);
+            executeSqlListWithLog(connection,
+                    "INSERT INTO t_order (order_id, user_id, status) VALUES (1, 1, '1');",
+                    "INSERT INTO t_order (order_id, user_id, status) VALUES (2, 2, '2');",
+                    "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (1, 1, 1, '1');",
+                    "INSERT INTO t_order_item (item_id, order_id, user_id, status) VALUES (2, 2, 2, '2');");
+            assertTableRowCount(connection, T_ORDER, 2);
+            assertTableRowCount(connection, T_ORDER_ITEM, 2);
+            connection.commit();
+            assertTableRowCount(connection, T_ORDER, 2);
+            assertTableRowCount(connection, T_ORDER_ITEM, 2);
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/SingleTableCommitAndRollbackTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/SingleTableCommitAndRollbackTestCase.java
index ba6a9d1d6a5..2d5cb408d2f 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/SingleTableCommitAndRollbackTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/commitrollback/SingleTableCommitAndRollbackTestCase.java
@@ -44,24 +44,26 @@ public final class SingleTableCommitAndRollbackTestCase extends BaseTransactionT
     }
     
     private void assertRollback() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 0);
-        Statement statement = connection.createStatement();
-        statement.execute("insert into account(id, balance, transaction_id) values(1, 1, 1);");
-        assertAccountRowCount(connection, 1);
-        connection.rollback();
-        assertAccountRowCount(connection, 0);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 0);
+            Statement statement = connection.createStatement();
+            statement.execute("insert into account(id, balance, transaction_id) values(1, 1, 1);");
+            assertAccountRowCount(connection, 1);
+            connection.rollback();
+            assertAccountRowCount(connection, 0);
+        }
     }
     
     private void assertCommit() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 0);
-        Statement statement = connection.createStatement();
-        statement.execute("insert into account(id, balance, transaction_id) values(1, 1, 1);");
-        assertAccountRowCount(connection, 1);
-        connection.commit();
-        assertAccountRowCount(connection, 1);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 0);
+            Statement statement = connection.createStatement();
+            statement.execute("insert into account(id, balance, transaction_id) values(1, 1, 1);");
+            assertAccountRowCount(connection, 1);
+            connection.commit();
+            assertAccountRowCount(connection, 1);
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/cursor/OpenGaussCursorTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/cursor/OpenGaussCursorTestCase.java
index 659540ce4eb..9e12d9fda63 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/cursor/OpenGaussCursorTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/cursor/OpenGaussCursorTestCase.java
@@ -57,20 +57,22 @@ public final class OpenGaussCursorTestCase extends BaseTransactionTestCase {
     
     @Override
     protected void beforeTest() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        assertTableRowCount(connection, "t_order", 4);
+        try (Connection connection = getDataSource().getConnection()) {
+            assertTableRowCount(connection, "t_order", 4);
+        }
     }
     
     @Override
     public void executeTest(final TransactionContainerComposer containerComposer) throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        singleTableCursorTest(connection);
-        singleTableCursorOrderByTest(connection);
-        broadcastTableCursorTest(connection);
-        broadcastTableCursorTest2(connection);
-        broadcastAndSingleTablesCursorTest(connection);
-        broadcastAndSingleTablesCursorTest2(connection);
-        viewCursorTest(connection);
+        try (Connection connection = getDataSource().getConnection()) {
+            singleTableCursorTest(connection);
+            singleTableCursorOrderByTest(connection);
+            broadcastTableCursorTest(connection);
+            broadcastTableCursorTest2(connection);
+            broadcastAndSingleTablesCursorTest(connection);
+            broadcastAndSingleTablesCursorTest2(connection);
+            viewCursorTest(connection);
+        }
     }
     
     private void singleTableCursorTest(final Connection connection) throws SQLException {
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/nested/NestedTransactionTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/nested/NestedTransactionTestCase.java
index b0afdc06402..768a5f0d99e 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/nested/NestedTransactionTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/nested/NestedTransactionTestCase.java
@@ -43,20 +43,22 @@ public class NestedTransactionTestCase extends BaseTransactionTestCase {
     
     @Override
     protected void executeTest(final TransactionContainerComposer containerComposer) throws SQLException {
-        ShardingSphereConnection connection = (ShardingSphereConnection) getDataSource().getConnection();
-        assertFalse(connection.isHoldTransaction());
-        connection.setAutoCommit(false);
-        assertTrue(connection.isHoldTransaction());
-        requiresNewTransaction();
-        assertTrue(connection.isHoldTransaction());
-        connection.commit();
+        try (ShardingSphereConnection connection = (ShardingSphereConnection) getDataSource().getConnection()) {
+            assertFalse(connection.isHoldTransaction());
+            connection.setAutoCommit(false);
+            assertTrue(connection.isHoldTransaction());
+            requiresNewTransaction();
+            assertTrue(connection.isHoldTransaction());
+            connection.commit();
+        }
     }
     
     private void requiresNewTransaction() throws SQLException {
-        ShardingSphereConnection connection = (ShardingSphereConnection) getDataSource().getConnection();
-        assertFalse(connection.isHoldTransaction());
-        connection.setAutoCommit(false);
-        assertTrue(connection.isHoldTransaction());
-        connection.commit();
+        try (ShardingSphereConnection connection = (ShardingSphereConnection) getDataSource().getConnection()) {
+            assertFalse(connection.isHoldTransaction());
+            connection.setAutoCommit(false);
+            assertTrue(connection.isHoldTransaction());
+            connection.commit();
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/MySQLSetReadOnlyTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/MySQLSetReadOnlyTestCase.java
index 1043ef62930..66708cea375 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/MySQLSetReadOnlyTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/MySQLSetReadOnlyTestCase.java
@@ -47,12 +47,12 @@ public final class MySQLSetReadOnlyTestCase extends SetReadOnlyTestCase {
     }
     
     private void assertSetReadOnly() throws SQLException {
-        Connection connection1 = getDataSource().getConnection();
-        executeUpdateWithLog(connection1, "insert into account(id, balance) values (1, 0), (2, 100);");
-        Connection connection2 = getDataSource().getConnection();
-        connection2.setReadOnly(true);
-        assertQueryBalance(connection2);
-        try {
+        try (Connection connection1 = getDataSource().getConnection()) {
+            executeUpdateWithLog(connection1, "insert into account(id, balance) values (1, 0), (2, 100);");
+        }
+        try (Connection connection2 = getDataSource().getConnection()) {
+            connection2.setReadOnly(true);
+            assertQueryBalance(connection2);
             executeWithLog(connection2, "update account set balance = 100 where id = 2;");
             fail("Update ran successfully, should failed.");
         } catch (final SQLException ex) {
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/PostgreSQLSetReadOnlyTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/PostgreSQLSetReadOnlyTestCase.java
index d1b550798cd..cb425907c93 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/PostgreSQLSetReadOnlyTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/PostgreSQLSetReadOnlyTestCase.java
@@ -46,12 +46,12 @@ public final class PostgreSQLSetReadOnlyTestCase extends SetReadOnlyTestCase {
     }
     
     private void assertSetReadOnly() throws SQLException {
-        Connection connection1 = getDataSource().getConnection();
-        executeUpdateWithLog(connection1, "insert into account(id, balance) values (1, 0), (2, 100);");
-        Connection connection2 = getDataSource().getConnection();
-        connection2.setReadOnly(true);
-        assertQueryBalance(connection2);
-        try {
+        try (Connection connection1 = getDataSource().getConnection()) {
+            executeUpdateWithLog(connection1, "insert into account(id, balance) values (1, 0), (2, 100);");
+        }
+        try (Connection connection2 = getDataSource().getConnection()) {
+            connection2.setReadOnly(true);
+            assertQueryBalance(connection2);
             executeWithLog(connection2, "update account set balance = 100 where id = 2;");
             log.info("Using the driver of postgresql:42.4.1 expect to update successfully.");
         } catch (final SQLException ex) {
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/SetReadOnlyTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/SetReadOnlyTestCase.java
index 13ee628c0c0..2e8896a9b27 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/SetReadOnlyTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/readonly/SetReadOnlyTestCase.java
@@ -39,13 +39,14 @@ public abstract class SetReadOnlyTestCase extends BaseTransactionTestCase {
     }
     
     void assertNotSetReadOnly() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        assertQueryBalance(connection);
-        executeUpdateWithLog(connection, "update account set balance = 101 where id = 2;");
-        ResultSet resultSet = executeQueryWithLog(connection, "select * from account where id = 2");
-        assertTrue(resultSet.next());
-        int balanceResult = resultSet.getInt("balance");
-        assertThat(String.format("Balance is %s, should be 101.", balanceResult), balanceResult, is(101));
+        try (Connection connection = getDataSource().getConnection()) {
+            assertQueryBalance(connection);
+            executeUpdateWithLog(connection, "update account set balance = 101 where id = 2;");
+            ResultSet resultSet = executeQueryWithLog(connection, "select * from account where id = 2");
+            assertTrue(resultSet.next());
+            int balanceResult = resultSet.getInt("balance");
+            assertThat(String.format("Balance is %s, should be 101.", balanceResult), balanceResult, is(101));    
+        }
     }
     
     void assertQueryBalance(final Connection connection) throws SQLException {
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/BaseSavePointTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/BaseSavePointTestCase.java
index 3e6eaa181aa..4bc354cb510 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/BaseSavePointTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/BaseSavePointTestCase.java
@@ -35,32 +35,34 @@ public abstract class BaseSavePointTestCase extends BaseTransactionTestCase {
     }
     
     void assertRollback2Savepoint() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 0);
-        executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1);");
-        final Savepoint savepoint = connection.setSavepoint("point1");
-        assertAccountRowCount(connection, 1);
-        executeWithLog(connection, "insert into account(id, balance, transaction_id) values(2, 2, 2);");
-        assertAccountRowCount(connection, 2);
-        connection.rollback(savepoint);
-        assertAccountRowCount(connection, 1);
-        connection.commit();
-        assertAccountRowCount(connection, 1);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 0);
+            executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1);");
+            final Savepoint savepoint = connection.setSavepoint("point1");
+            assertAccountRowCount(connection, 1);
+            executeWithLog(connection, "insert into account(id, balance, transaction_id) values(2, 2, 2);");
+            assertAccountRowCount(connection, 2);
+            connection.rollback(savepoint);
+            assertAccountRowCount(connection, 1);
+            connection.commit();
+            assertAccountRowCount(connection, 1);
+        }
     }
     
     void assertReleaseSavepoint() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 1);
-        executeWithLog(connection, "insert into account(id, balance, transaction_id) values(2, 2, 2);");
-        final Savepoint savepoint = connection.setSavepoint("point2");
-        assertAccountRowCount(connection, 2);
-        executeWithLog(connection, "insert into account(id, balance, transaction_id) values(3, 3, 3);");
-        assertAccountRowCount(connection, 3);
-        connection.releaseSavepoint(savepoint);
-        assertAccountRowCount(connection, 3);
-        connection.commit();
-        assertAccountRowCount(connection, 3);
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 1);
+            executeWithLog(connection, "insert into account(id, balance, transaction_id) values(2, 2, 2);");
+            final Savepoint savepoint = connection.setSavepoint("point2");
+            assertAccountRowCount(connection, 2);
+            executeWithLog(connection, "insert into account(id, balance, transaction_id) values(3, 3, 3);");
+            assertAccountRowCount(connection, 3);
+            connection.releaseSavepoint(savepoint);
+            assertAccountRowCount(connection, 3);
+            connection.commit();
+            assertAccountRowCount(connection, 3);
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/OpenGaussSavePointTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/OpenGaussSavePointTestCase.java
index 55ab0ff036a..cddd2fda1a1 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/OpenGaussSavePointTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/OpenGaussSavePointTestCase.java
@@ -50,24 +50,25 @@ public final class OpenGaussSavePointTestCase extends BaseSavePointTestCase {
     }
     
     private void assertErrors() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        try {
-            connection.setSavepoint("point");
-            fail("Expect exception, but no exception report.");
-        } catch (final SQLException ex) {
-            assertThat(ex.getMessage(), is("Cannot establish a savepoint in auto-commit mode."));
-        }
-        try {
-            connection.rollback(new PSQLSavepoint("point1"));
-            fail("Expect exception, but no exception report.");
-        } catch (final SQLException ex) {
-            assertTrue(ex.getMessage().endsWith("ERROR: ROLLBACK TO SAVEPOINT can only be used in transaction blocks"));
-        }
-        try {
-            connection.releaseSavepoint(new PSQLSavepoint("point1"));
-            fail("Expect exception, but no exception report.");
-        } catch (final SQLException ex) {
-            assertTrue(ex.getMessage().endsWith("ERROR: RELEASE SAVEPOINT can only be used in transaction blocks"));
+        try (Connection connection = getDataSource().getConnection()) {
+            try {
+                connection.setSavepoint("point");
+                fail("Expect exception, but no exception report.");
+            } catch (final SQLException ex) {
+                assertThat(ex.getMessage(), is("Cannot establish a savepoint in auto-commit mode."));
+            }
+            try {
+                connection.rollback(new PSQLSavepoint("point1"));
+                fail("Expect exception, but no exception report.");
+            } catch (final SQLException ex) {
+                assertTrue(ex.getMessage().endsWith("ERROR: ROLLBACK TO SAVEPOINT can only be used in transaction blocks"));
+            }
+            try {
+                connection.releaseSavepoint(new PSQLSavepoint("point1"));
+                fail("Expect exception, but no exception report.");
+            } catch (final SQLException ex) {
+                assertTrue(ex.getMessage().endsWith("ERROR: RELEASE SAVEPOINT can only be used in transaction blocks"));
+            }
         }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/PostgreSQLSavePointTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/PostgreSQLSavePointTestCase.java
index 86f2f54e87a..2013c8c9062 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/PostgreSQLSavePointTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/savepoint/PostgreSQLSavePointTestCase.java
@@ -52,26 +52,27 @@ public final class PostgreSQLSavePointTestCase extends BaseSavePointTestCase {
     
     @SneakyThrows(SQLException.class)
     private void assertErrors() {
-        Connection connection = getDataSource().getConnection();
-        try {
-            connection.setSavepoint("point");
-            fail("Expect exception, but no exception report.");
-        } catch (final SQLException ex) {
-            assertThat(ex.getMessage(), is("Savepoint can only be used in transaction blocks."));
-        }
-        try {
-            connection.rollback(new PSQLSavepoint("point1"));
-            fail("Expect exception, but no exception report.");
-        } catch (final SQLException ex) {
-            // TODO can not run to get the correct result in JDBC mode.
-            assertTrue(ex.getMessage().endsWith("ERROR: ROLLBACK TO SAVEPOINT can only be used in transaction blocks"));
-        }
-        try {
-            connection.releaseSavepoint(new PSQLSavepoint("point1"));
-            fail("Expect exception, but no exception report.");
-        } catch (final SQLException ex) {
-            // TODO can not run to get the correct result in JDBC mode.
-            assertTrue(ex.getMessage().endsWith("ERROR: RELEASE SAVEPOINT can only be used in transaction blocks"));
+        try (Connection connection = getDataSource().getConnection()) {
+            try {
+                connection.setSavepoint("point");
+                fail("Expect exception, but no exception report.");
+            } catch (final SQLException ex) {
+                assertThat(ex.getMessage(), is("Savepoint can only be used in transaction blocks."));
+            }
+            try {
+                connection.rollback(new PSQLSavepoint("point1"));
+                fail("Expect exception, but no exception report.");
+            } catch (final SQLException ex) {
+                // TODO can not run to get the correct result in JDBC mode.
+                assertTrue(ex.getMessage().endsWith("ERROR: ROLLBACK TO SAVEPOINT can only be used in transaction blocks"));
+            }
+            try {
+                connection.releaseSavepoint(new PSQLSavepoint("point1"));
+                fail("Expect exception, but no exception report.");
+            } catch (final SQLException ex) {
+                // TODO can not run to get the correct result in JDBC mode.
+                assertTrue(ex.getMessage().endsWith("ERROR: RELEASE SAVEPOINT can only be used in transaction blocks"));
+            }
         }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLLocalTruncateTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLLocalTruncateTestCase.java
index 81d657cb1c7..ec52db3d729 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLLocalTruncateTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLLocalTruncateTestCase.java
@@ -46,33 +46,33 @@ public final class MySQLLocalTruncateTestCase extends BaseTransactionTestCase {
     
     private void assertTruncateRollback() throws SQLException {
         prepare();
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 8);
-        executeWithLog(connection, "truncate account;");
-        assertAccountRowCount(connection, 0);
-        connection.rollback();
-        // Expected truncate operation cannot be rolled back in MySQL local transaction
-        assertAccountRowCount(connection, 0);
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 8);
+            executeWithLog(connection, "truncate account;");
+            assertAccountRowCount(connection, 0);
+            connection.rollback();
+            // Expected truncate operation cannot be rolled back in MySQL local transaction
+            assertAccountRowCount(connection, 0);
+        }
     }
     
     private void assertTruncateCommit() throws SQLException {
         prepare();
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 8);
-        executeWithLog(connection, "truncate account;");
-        assertAccountRowCount(connection, 0);
-        connection.commit();
-        assertAccountRowCount(connection, 0);
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 8);
+            executeWithLog(connection, "truncate account;");
+            assertAccountRowCount(connection, 0);
+            connection.commit();
+            assertAccountRowCount(connection, 0);
+        }
     }
     
     private void prepare() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        executeWithLog(connection, "delete from account;");
-        executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6),(7, 7, 7),(8, 8, 8);");
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            executeWithLog(connection, "delete from account;");
+            executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6),(7, 7, 7),(8, 8, 8);");
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLXATruncateTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLXATruncateTestCase.java
index bb889e59b6c..5ad018bc7c3 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLXATruncateTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/MySQLXATruncateTestCase.java
@@ -50,10 +50,10 @@ public final class MySQLXATruncateTestCase extends BaseTransactionTestCase {
     }
     
     private void prepare() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        executeWithLog(connection, "delete from account;");
-        executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6),(7, 7, 7),(8, 8, 8);");
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            executeWithLog(connection, "delete from account;");
+            executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6),(7, 7, 7),(8, 8, 8);");
+        }
     }
     
     @Override
@@ -63,19 +63,19 @@ public final class MySQLXATruncateTestCase extends BaseTransactionTestCase {
     
     private void assertTruncateInMySQLXATransaction() throws SQLException {
         // TODO This test case may cause bad effects to other test cases in JDBC adapter
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 8);
-        try {
-            connection.createStatement().execute("truncate account;");
-            fail("Expect exception, but no exception report.");
-        } catch (final TableModifyInTransactionException ex) {
-            log.info("Exception for expected in Proxy: {}", ex.getMessage());
-        } catch (final SQLException ex) {
-            log.info("Exception for expected in JDBC: {}", ex.getMessage());
-        } finally {
-            connection.rollback();
-            connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 8);
+            try {
+                connection.createStatement().execute("truncate account;");
+                fail("Expect exception, but no exception report.");
+            } catch (final TableModifyInTransactionException ex) {
+                log.info("Exception for expected in Proxy: {}", ex.getMessage());
+            } catch (final SQLException ex) {
+                log.info("Exception for expected in JDBC: {}", ex.getMessage());
+            } finally {
+                connection.rollback();
+            }
         }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/PostgreSQLAndOpenGaussTruncateTestCase.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/PostgreSQLAndOpenGaussTruncateTestCase.java
index 007c842ecb4..f855c0c2b4f 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/PostgreSQLAndOpenGaussTruncateTestCase.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/cases/truncate/PostgreSQLAndOpenGaussTruncateTestCase.java
@@ -45,35 +45,35 @@ public final class PostgreSQLAndOpenGaussTruncateTestCase extends BaseTransactio
     
     private void assertTruncateRollback() throws SQLException {
         prepare();
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 8);
-        executeWithLog(connection, "truncate account;");
-        assertAccountRowCount(connection, 0);
-        connection.rollback();
-        // Expected truncate operation can be rolled back in PostgreSQL & OpenGauss
-        assertAccountRowCount(connection, 8);
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 8);
+            executeWithLog(connection, "truncate account;");
+            assertAccountRowCount(connection, 0);
+            connection.rollback();
+            // Expected truncate operation can be rolled back in PostgreSQL & OpenGauss
+            assertAccountRowCount(connection, 8);
+        }
     }
     
     private void assertTruncateCommit() throws SQLException {
         prepare();
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        assertAccountRowCount(connection, 8);
-        executeWithLog(connection, "truncate account;");
-        assertAccountRowCount(connection, 0);
-        connection.commit();
-        assertAccountRowCount(connection, 0);
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            assertAccountRowCount(connection, 8);
+            executeWithLog(connection, "truncate account;");
+            assertAccountRowCount(connection, 0);
+            connection.commit();
+            assertAccountRowCount(connection, 0);
+        }
     }
     
     private void prepare() throws SQLException {
-        Connection connection = getDataSource().getConnection();
-        connection.setAutoCommit(false);
-        executeWithLog(connection, "delete from account;");
-        executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6),(7, 7, 7),(8, 8, 8);");
-        connection.commit();
-        connection.close();
+        try (Connection connection = getDataSource().getConnection()) {
+            connection.setAutoCommit(false);
+            executeWithLog(connection, "delete from account;");
+            executeWithLog(connection, "insert into account(id, balance, transaction_id) values(1, 1, 1),(2, 2, 2),(3, 3, 3),(4, 4, 4),(5, 5, 5),(6, 6, 6),(7, 7, 7),(8, 8, 8);");
+            connection.commit();
+        }
     }
 }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/engine/base/TransactionBaseE2EIT.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/engine/base/TransactionBaseE2EIT.java
index 0ca02237d75..c13bf821c4b 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/engine/base/TransactionBaseE2EIT.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/engine/base/TransactionBaseE2EIT.java
@@ -194,22 +194,24 @@ public abstract class TransactionBaseE2EIT {
     }
     
     private void alterLocalTransactionRule(final TransactionContainerComposer containerComposer) throws SQLException {
-        Connection connection = containerComposer.getDataSource().getConnection();
-        if (isExpectedTransactionRule(connection, TransactionType.LOCAL, "")) {
-            return;
+        try (Connection connection = containerComposer.getDataSource().getConnection()) {
+            if (isExpectedTransactionRule(connection, TransactionType.LOCAL, "")) {
+                return;
+            }
+            String alterLocalTransactionRule = commonSQL.getAlterLocalTransactionRule();
+            executeWithLog(connection, alterLocalTransactionRule);    
         }
-        String alterLocalTransactionRule = commonSQL.getAlterLocalTransactionRule();
-        executeWithLog(connection, alterLocalTransactionRule);
         assertTrue(waitExpectedTransactionRule(TransactionType.LOCAL, "", containerComposer));
     }
     
     private void alterXaTransactionRule(final String providerType, final TransactionContainerComposer containerComposer) throws SQLException {
-        Connection connection = containerComposer.getDataSource().getConnection();
-        if (isExpectedTransactionRule(connection, TransactionType.XA, providerType)) {
-            return;
+        try (Connection connection = containerComposer.getDataSource().getConnection()) {
+            if (isExpectedTransactionRule(connection, TransactionType.XA, providerType)) {
+                return;
+            }
+            String alterXaTransactionRule = commonSQL.getAlterXATransactionRule().replace("${providerType}", providerType);
+            executeWithLog(connection, alterXaTransactionRule);
         }
-        String alterXaTransactionRule = commonSQL.getAlterXATransactionRule().replace("${providerType}", providerType);
-        executeWithLog(connection, alterXaTransactionRule);
         assertTrue(waitExpectedTransactionRule(TransactionType.XA, providerType, containerComposer));
     }
     
@@ -221,16 +223,17 @@ public abstract class TransactionBaseE2EIT {
     
     private boolean waitExpectedTransactionRule(final TransactionType expectedTransType, final String expectedProviderType, final TransactionContainerComposer containerComposer) throws SQLException {
         ThreadUtil.sleep(5, TimeUnit.SECONDS);
-        Connection connection = containerComposer.getDataSource().getConnection();
-        int waitTimes = 0;
-        do {
-            if (isExpectedTransactionRule(connection, expectedTransType, expectedProviderType)) {
-                return true;
-            }
-            ThreadUtil.sleep(2, TimeUnit.SECONDS);
-            waitTimes++;
-        } while (waitTimes <= 3);
-        return false;
+        try (Connection connection = containerComposer.getDataSource().getConnection()) {
+            int waitTimes = 0;
+            do {
+                if (isExpectedTransactionRule(connection, expectedTransType, expectedProviderType)) {
+                    return true;
+                }
+                ThreadUtil.sleep(2, TimeUnit.SECONDS);
+                waitTimes++;
+            } while (waitTimes <= 3);
+            return false;
+        }
     }
     
     private Map<String, String> executeShowTransactionRule(final Connection connection) throws SQLException {
@@ -296,22 +299,23 @@ public abstract class TransactionBaseE2EIT {
     }
     
     private int countWithLog(final String sql, final TransactionContainerComposer containerComposer) throws SQLException {
-        Connection connection = containerComposer.getDataSource().getConnection();
-        int retryNumber = 0;
-        while (retryNumber <= 3) {
-            try {
-                Statement statement = connection.createStatement();
-                ResultSet resultSet = statement.executeQuery(sql);
-                int result = 0;
-                while (resultSet.next()) {
-                    result++;
+        try (Connection connection = containerComposer.getDataSource().getConnection()) {
+            int retryNumber = 0;
+            while (retryNumber <= 3) {
+                try {
+                    Statement statement = connection.createStatement();
+                    ResultSet resultSet = statement.executeQuery(sql);
+                    int result = 0;
+                    while (resultSet.next()) {
+                        result++;
+                    }
+                    return result;
+                } catch (final SQLException ex) {
+                    log.error("Data access error.", ex);
                 }
-                return result;
-            } catch (final SQLException ex) {
-                log.error("Data access error.", ex);
+                ThreadUtil.sleep(2, TimeUnit.SECONDS);
+                retryNumber++;
             }
-            ThreadUtil.sleep(2, TimeUnit.SECONDS);
-            retryNumber++;
         }
         throw new RuntimeException("Can't get result from proxy.");
     }
diff --git a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/engine/base/TransactionContainerComposer.java b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/engine/base/TransactionContainerComposer.java
index d1381bc60fd..500c98a8684 100644
--- a/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/engine/base/TransactionContainerComposer.java
+++ b/test/e2e/transaction/src/test/java/org/apache/shardingsphere/test/e2e/transaction/engine/base/TransactionContainerComposer.java
@@ -59,7 +59,7 @@ public final class TransactionContainerComposer implements AutoCloseable {
         return result;
     }
     
-    boolean isProxyAdapter(final TransactionTestParameter testParam) {
+    private boolean isProxyAdapter(final TransactionTestParameter testParam) {
         return AdapterType.PROXY.getValue().equalsIgnoreCase(testParam.getAdapter());
     }