You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/11/22 07:21:37 UTC

[shardingsphere] branch master updated: JDBCBackendDataSource.createConnection may occurs NullPointException (#13731)

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

menghaoran 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 dd7b9dc  JDBCBackendDataSource.createConnection may occurs NullPointException (#13731)
dd7b9dc is described below

commit dd7b9dc1939c0b4fc33f806291f913e07a91587a
Author: sunhangda <lw...@126.com>
AuthorDate: Mon Nov 22 15:20:58 2021 +0800

    JDBCBackendDataSource.createConnection may occurs NullPointException (#13731)
    
    when changing dataSource metadata.
---
 .../transaction/context/TransactionContexts.java             |  4 ++--
 .../apache/shardingsphere/mode/manager/ContextManager.java   | 12 ++++++++----
 .../coordinator/ClusterContextManagerCoordinator.java        |  2 --
 3 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/context/TransactionContexts.java b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/context/TransactionContexts.java
index 8626386..5b21289 100644
--- a/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/context/TransactionContexts.java
+++ b/shardingsphere-kernel/shardingsphere-transaction/shardingsphere-transaction-core/src/main/java/org/apache/shardingsphere/transaction/context/TransactionContexts.java
@@ -21,8 +21,8 @@ import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.transaction.ShardingSphereTransactionManagerEngine;
 
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Transaction contexts.
@@ -34,7 +34,7 @@ public final class TransactionContexts implements AutoCloseable {
     private final Map<String, ShardingSphereTransactionManagerEngine> engines;
     
     public TransactionContexts() {
-        this(new HashMap<>());
+        this(new ConcurrentHashMap<>());
     }
     
     @Override
diff --git a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index d936a01..0acc65b 100644
--- a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++ b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -136,7 +136,7 @@ public final class ContextManager implements AutoCloseable {
             metaDataContexts.getOptimizerContext().getPlannerContexts().remove(schemaName);
             ShardingSphereMetaData removeMetaData = metaDataContexts.getMetaDataMap().remove(schemaName);
             closeDataSources(removeMetaData);
-            closeTransactionEngine(schemaName);
+            removeAndCloseTransactionEngine(schemaName);
         }
     }
     
@@ -367,8 +367,8 @@ public final class ContextManager implements AutoCloseable {
     }
     
     private void renewTransactionContext(final String schemaName, final ShardingSphereResource resource) {
-        closeTransactionEngine(schemaName);
-        transactionContexts.getEngines().put(schemaName, createNewEngine(resource.getDatabaseType(), resource.getDataSources()));
+        ShardingSphereTransactionManagerEngine changedStaleEngine = transactionContexts.getEngines().put(schemaName, createNewEngine(resource.getDatabaseType(), resource.getDataSources()));
+        closeTransactionEngine(changedStaleEngine);
     }
     
     private ShardingSphereTransactionManagerEngine createNewEngine(final DatabaseType databaseType, final Map<String, DataSource> dataSources) {
@@ -412,8 +412,12 @@ public final class ContextManager implements AutoCloseable {
         }
     }
     
-    private void closeTransactionEngine(final String schemaName) {
+    private void removeAndCloseTransactionEngine(final String schemaName) {
         ShardingSphereTransactionManagerEngine staleEngine = transactionContexts.getEngines().remove(schemaName);
+        closeTransactionEngine(staleEngine);
+    }
+    
+    private void closeTransactionEngine(final ShardingSphereTransactionManagerEngine staleEngine) {
         if (null != staleEngine) {
             try {
                 staleEngine.close();
diff --git a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
index ef09e15..1006b0f 100644
--- a/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
+++ b/shardingsphere-mode/shardingsphere-mode-type/shardingsphere-cluster-mode/shardingsphere-cluster-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/coordinator/ClusterContextManagerCoordinator.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.mode.manager.cluster.coordinator;
 
 import com.google.common.eventbus.Subscribe;
-import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.authority.rule.AuthorityRule;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 import org.apache.shardingsphere.infra.metadata.schema.QualifiedSchema;
@@ -46,7 +45,6 @@ import java.util.Optional;
 /**
  * Cluster context manager coordinator.
  */
-@Slf4j
 public final class ClusterContextManagerCoordinator {
     
     private final MetaDataPersistService metaDataPersistService;