You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/09/11 14:31:17 UTC

[shardingsphere] branch master updated: Add sql execution release template method and adjust logic. (#12207)

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

panjuan 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 73ec44d  Add sql execution release template method and adjust logic. (#12207)
73ec44d is described below

commit 73ec44d43faabe30579d454b86acb8632c8117f1
Author: Guocheng Tang <to...@qq.com>
AuthorDate: Sat Sep 11 22:30:45 2021 +0800

    Add sql execution release template method and adjust logic. (#12207)
    
    * Add sql execution release template method, adjust logic.
    
    * Catch ignored exception.
    
    * Improve resource release logic.
    
    * Improve resource release logic.
---
 .../scaling/test/mysql/fixture/DataImporter.java   |  5 ++-
 .../test/integration/engine/it/BaseITCase.java     | 52 ++++++++++++++++++++++
 .../integration/engine/it/dcl/GeneralDCLIT.java    | 10 +++--
 .../test/integration/engine/it/ddl/BaseDDLIT.java  | 14 ++----
 .../test/integration/engine/it/dml/BatchDMLIT.java | 13 +++---
 5 files changed, 71 insertions(+), 23 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-scaling-test/shardingsphere-integration-scaling-test-mysql/src/test/java/org/apache/shardingsphere/integration/scaling/test/mysql/fixture/DataImporter.java b/shardingsphere-test/shardingsphere-integration-scaling-test/shardingsphere-integration-scaling-test-mysql/src/test/java/org/apache/shardingsphere/integration/scaling/test/mysql/fixture/DataImporter.java
index e132a6d..2d29982 100644
--- a/shardingsphere-test/shardingsphere-integration-scaling-test/shardingsphere-integration-scaling-test-mysql/src/test/java/org/apache/shardingsphere/integration/scaling/test/mysql/fixture/DataImporter.java
+++ b/shardingsphere-test/shardingsphere-integration-scaling-test/shardingsphere-integration-scaling-test-mysql/src/test/java/org/apache/shardingsphere/integration/scaling/test/mysql/fixture/DataImporter.java
@@ -55,8 +55,9 @@ public final class DataImporter {
     
     @SneakyThrows(SQLException.class)
     private void createTable(final DataSource dataSource, final String tableName, final String columnName, final String columnType) {
-        try (Connection connection = dataSource.getConnection()) {
-            connection.prepareStatement(String.format(CREATE_SQL, tableName, columnName, columnType)).execute();
+        try (Connection connection = dataSource.getConnection();
+             PreparedStatement preparedStatement = connection.prepareStatement(String.format(CREATE_SQL, tableName, columnName, columnType))) {
+            preparedStatement.execute();
         }
     }
     
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseITCase.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseITCase.java
index 13f6163..937ed05 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseITCase.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/BaseITCase.java
@@ -36,6 +36,10 @@ import org.junit.runner.RunWith;
 
 import javax.sql.DataSource;
 import java.io.IOException;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Statement;
 import java.text.ParseException;
 import java.util.Map;
 import java.util.TimeZone;
@@ -105,4 +109,52 @@ public abstract class BaseITCase {
     }
     
     protected abstract String getSQL() throws ParseException;
+
+    /**
+     * execute sql update for statement.
+     * @param connection datasource connection
+     * @param sql SQL statement executed
+     * @throws SQLException sql execute exception.
+     */
+    protected void executeUpdateForStatement(final Connection connection, final String sql) throws SQLException {
+        try (Statement statement = connection.createStatement()) {
+            statement.executeUpdate(sql);
+        }
+    }
+    
+    /**
+     * execute sql update for prepareStatement.
+     * @param connection datasource connection
+     * @param sql SQL statement executed
+     * @throws SQLException sql execute exception.
+     */
+    protected void executeUpdateForPrepareStatement(final Connection connection, final String sql) throws SQLException {
+        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
+            preparedStatement.executeUpdate();
+        }
+    }
+
+    /**
+     * execute sql for statement.
+     * @param connection datasource connection
+     * @param sql SQL statement executed
+     * @throws SQLException sql execute exception.
+     */
+    protected void executeForStatement(final Connection connection, final String sql) throws SQLException {
+        try (Statement statement = connection.createStatement()) {
+            statement.execute(sql);
+        }
+    }
+
+    /**
+     * execute sql for prepareStatement.
+     * @param connection datasource connection
+     * @param sql SQL statement executed
+     * @throws SQLException sql execute exception.
+     */
+    protected void executeForPrepareStatement(final Connection connection, final String sql) throws SQLException {
+        try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
+            preparedStatement.execute();
+        }
+    }
 }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dcl/GeneralDCLIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dcl/GeneralDCLIT.java
index bb9832c..46fe2b1 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dcl/GeneralDCLIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dcl/GeneralDCLIT.java
@@ -55,22 +55,24 @@ public final class GeneralDCLIT extends BaseDCLIT {
     
     @Test
     public void assertExecuteUpdate() throws SQLException, ParseException {
+        String sql = getSQL();
         try (Connection connection = getTargetDataSource().getConnection()) {
             if (SQLExecuteType.Literal == getSqlExecuteType()) {
-                connection.createStatement().executeUpdate(getSQL());
+                executeUpdateForStatement(connection, sql);
             } else {
-                connection.prepareStatement(getSQL()).executeUpdate();
+                executeUpdateForPrepareStatement(connection, sql);
             }
         }
     }
     
     @Test
     public void assertExecute() throws SQLException, ParseException {
+        String sql = getSQL();
         try (Connection connection = getTargetDataSource().getConnection()) {
             if (SQLExecuteType.Literal == getSqlExecuteType()) {
-                connection.createStatement().execute(getSQL());
+                executeForStatement(connection, sql);
             } else {
-                connection.prepareStatement(getSQL()).execute();
+                executeForPrepareStatement(connection, sql);
             }
         }
     }
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/ddl/BaseDDLIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/ddl/BaseDDLIT.java
index ecead5e..b4dce9c 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/ddl/BaseDDLIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/ddl/BaseDDLIT.java
@@ -34,7 +34,6 @@ import org.apache.shardingsphere.test.integration.junit.param.model.AssertionPar
 import java.io.IOException;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
-import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Collection;
@@ -77,7 +76,9 @@ public abstract class BaseDDLIT extends SingleITCase {
     public final void tearDown() throws Exception {
         dataSetEnvironmentManager.clearData();
         try (Connection connection = getTargetDataSource().getConnection()) {
-            dropInitializedTable(connection);
+            String dropSql = String.format("DROP TABLE %s", getAssertion().getInitialSQL().getAffectedTable());
+            executeUpdateForPrepareStatement(connection, dropSql);
+        } catch (final SQLException | NoSuchTableException ignored) {
         }
         super.tearDown();
     }
@@ -87,14 +88,7 @@ public abstract class BaseDDLIT extends SingleITCase {
             return;
         }
         for (String each : Splitter.on(";").trimResults().splitToList(getAssertion().getInitialSQL().getSql())) {
-            connection.prepareStatement(each).executeUpdate();
-        }
-    }
-    
-    private void dropInitializedTable(final Connection connection) {
-        try (PreparedStatement preparedStatement = connection.prepareStatement(String.format("DROP TABLE %s", getAssertion().getInitialSQL().getAffectedTable()))) {
-            preparedStatement.executeUpdate();
-        } catch (final SQLException | NoSuchTableException ignored) {
+            executeUpdateForPrepareStatement(connection, each);
         }
     }
     
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/BatchDMLIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/BatchDMLIT.java
index fa4bcef..f992e01 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/BatchDMLIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/it/dml/BatchDMLIT.java
@@ -101,14 +101,13 @@ public final class BatchDMLIT extends BatchITCase {
                 return;
             default:
         }
-        try (Connection connection = getTargetDataSource().getConnection()) {
-            try (PreparedStatement preparedStatement = connection.prepareStatement(getSQL())) {
-                for (IntegrationTestCaseAssertion each : getIntegrationTestCase().getAssertions()) {
-                    addBatch(preparedStatement, each);
-                }
-                preparedStatement.clearBatch();
-                assertThat(preparedStatement.executeBatch().length, is(0));
+        try (Connection connection = getTargetDataSource().getConnection();
+             PreparedStatement preparedStatement = connection.prepareStatement(getSQL())) {
+            for (IntegrationTestCaseAssertion each : getIntegrationTestCase().getAssertions()) {
+                addBatch(preparedStatement, each);
             }
+            preparedStatement.clearBatch();
+            assertThat(preparedStatement.executeBatch().length, is(0));
         }
     }
 }