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 3c51038dfed Close created data sources if create next data source 
failed on DataSourceGeneratedDatabaseConfiguration (#31011)
3c51038dfed is described below

commit 3c51038dfedf2a4e43798ec5df41dcf9e4811061
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Apr 25 19:08:48 2024 +0800

    Close created data sources if create next data source failed on 
DataSourceGeneratedDatabaseConfiguration (#31011)
---
 .../DataSourceGeneratedDatabaseConfiguration.java  | 25 ++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java
index 31c28a144ac..9a60293ca24 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java
@@ -24,11 +24,13 @@ import 
org.apache.shardingsphere.infra.datasource.pool.config.DataSourceConfigur
 import 
org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
 import 
org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
 import 
org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
+import 
org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException;
 import 
org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
 import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
 import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapCreator;
 
 import javax.sql.DataSource;
+import java.sql.SQLException;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.Map;
@@ -66,9 +68,28 @@ public final class DataSourceGeneratedDatabaseConfiguration 
implements DatabaseC
     
     private Map<StorageNode, DataSource> getStorageNodeDataSourceMap(final 
Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final 
Map<String, StorageNode> storageUnitNodeMap) {
         Map<StorageNode, DataSource> result = new 
LinkedHashMap<>(storageUnitNodeMap.size(), 1F);
-        for (Entry<String, StorageNode> entry : storageUnitNodeMap.entrySet()) 
{
-            result.computeIfAbsent(entry.getValue(), key -> 
DataSourcePoolCreator.create(entry.getKey(), 
dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values()));
+        try {
+            for (Entry<String, StorageNode> entry : 
storageUnitNodeMap.entrySet()) {
+                result.computeIfAbsent(entry.getValue(), key -> 
DataSourcePoolCreator.create(entry.getKey(), 
dataSourcePoolPropertiesMap.get(entry.getKey()), true, result.values()));
+            }
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            result.values().forEach(this::close);
+            throw ex;
         }
         return result;
     }
+    
+    private void close(final DataSource dataSource) {
+        if (dataSource instanceof AutoCloseable) {
+            try {
+                ((AutoCloseable) dataSource).close();
+                // CHECKSTYLE:OFF
+            } catch (final Exception ex) {
+                // CHECKSTYLE:ON
+                throw new SQLWrapperException(new SQLException(ex));
+            }
+        }
+    }
 }

Reply via email to