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/04/07 10:32:16 UTC

[shardingsphere] branch master updated: Refactor ShardingSphereDataSource.close() to throw SQLException (#25056)

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

zhaojinchao 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 e48fa53cca7 Refactor ShardingSphereDataSource.close() to throw SQLException (#25056)
e48fa53cca7 is described below

commit e48fa53cca73c3639e3fafb5b37547be8a74466a
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Apr 7 18:32:06 2023 +0800

    Refactor ShardingSphereDataSource.close() to throw SQLException (#25056)
---
 .../jdbc/core/datasource/ShardingSphereDataSource.java   | 16 +++++++++++-----
 .../core/datasource/ShardingSphereDataSourceTest.java    |  2 +-
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index 3172ff044cf..73789e034f9 100644
--- a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++ b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -107,10 +107,10 @@ public final class ShardingSphereDataSource extends AbstractDataSourceAdapter im
      * Close data sources.
      *
      * @param dataSourceNames data source names to be closed
-     * @throws Exception exception
+     * @throws SQLException SQL exception
      */
     // TODO Replace public to private?
-    public void close(final Collection<String> dataSourceNames) throws Exception {
+    public void close(final Collection<String> dataSourceNames) throws SQLException {
         Map<String, DataSource> dataSourceMap = contextManager.getDataSourceMap(databaseName);
         for (String each : dataSourceNames) {
             close(dataSourceMap.get(each));
@@ -118,14 +118,20 @@ public final class ShardingSphereDataSource extends AbstractDataSourceAdapter im
         contextManager.close();
     }
     
-    private void close(final DataSource dataSource) throws Exception {
+    private void close(final DataSource dataSource) throws SQLException {
         if (dataSource instanceof AutoCloseable) {
-            ((AutoCloseable) dataSource).close();
+            try {
+                ((AutoCloseable) dataSource).close();
+                // CHECKSTYLE:OFF
+            } catch (final Exception ex) {
+                // CHECKSTYLE:ON
+                throw new SQLException(ex);
+            }
         }
     }
     
     @Override
-    public void close() throws Exception {
+    public void close() throws SQLException {
         contextManagerDestroyedCallback(databaseName);
         close(contextManager.getDataSourceMap(databaseName).keySet());
     }
diff --git a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
index dec42e30eb3..0effe85a42b 100644
--- a/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
+++ b/jdbc/core/src/test/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSourceTest.java
@@ -148,7 +148,7 @@ class ShardingSphereDataSourceTest {
     }
     
     @Test
-    void assertCloseWithDataSourceNames() throws Exception {
+    void assertCloseWithDataSourceNames() throws SQLException {
         try (HikariDataSource dataSource = createHikariDataSource()) {
             ShardingSphereDataSource actual = createShardingSphereDataSource(dataSource);
             actual.close(Collections.singleton("ds"));