From e324efb53e4d32221e1bfb5f22ca35391571984b Mon Sep 17 00:00:00 2001 From: Karen Chen Date: Wed, 15 Mar 2023 15:34:22 -0700 Subject: [PATCH] refactor: reduce map lookups to reduce the number of locks required --- .../core-api/java/com/mysql/cj/util/Util.java | 5 ++- .../failover/FailoverConnectionPlugin.java | 37 ++++++++++--------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/main/core-api/java/com/mysql/cj/util/Util.java b/src/main/core-api/java/com/mysql/cj/util/Util.java index acdd86085..e7a91082a 100644 --- a/src/main/core-api/java/com/mysql/cj/util/Util.java +++ b/src/main/core-api/java/com/mysql/cj/util/Util.java @@ -307,8 +307,9 @@ public static List loadClasses(String extensionClassNames, String errorMe * @return boolean */ public static boolean isJdbcInterface(Class clazz) { - if (Util.isJdbcInterfaceCache.containsKey(clazz)) { - return (Util.isJdbcInterfaceCache.get(clazz)); + final Boolean isJdbcInterface = Util.isJdbcInterfaceCache.get(clazz); + if (isJdbcInterface != null) { + return isJdbcInterface; } if (clazz.isInterface()) { diff --git a/src/main/user-impl/java/com/mysql/cj/jdbc/ha/plugins/failover/FailoverConnectionPlugin.java b/src/main/user-impl/java/com/mysql/cj/jdbc/ha/plugins/failover/FailoverConnectionPlugin.java index ff06a8b70..8ba4969cc 100644 --- a/src/main/user-impl/java/com/mysql/cj/jdbc/ha/plugins/failover/FailoverConnectionPlugin.java +++ b/src/main/user-impl/java/com/mysql/cj/jdbc/ha/plugins/failover/FailoverConnectionPlugin.java @@ -61,16 +61,19 @@ import java.io.EOFException; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Properties; +import java.util.Set; import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * A {@link IConnectionPlugin} implementation that provides cluster-aware failover @@ -92,22 +95,22 @@ public class FailoverConnectionPlugin implements IConnectionPlugin { static final String METHOD_CLOSE = "close"; static final String METHOD_IS_CLOSED = "isClosed"; - private static final List METHODS_REQUIRE_UPDATED_TOPOLOGY = new ArrayList<>(Arrays.asList( - METHOD_COMMIT, - "connect", - "isValid", - "rollback", - "setAutoCommit", - "setReadOnly", - "execute", - "executeBatch", - "executeLargeBatch", - "executeLargeUpdate", - "executeQuery", - "executeUpdate", - "executeWithFlags", - "getParameterMetaData" - )); + private final static Set METHODS_REQUIRE_UPDATED_TOPOLOGY = new ConcurrentHashMap() {{ + put(METHOD_COMMIT, METHOD_COMMIT); + put("connect", "connect"); + put("isValid", "isValid"); + put("rollback", "rollback"); + put("setAutoCommit", "setAutoCommit"); + put("setReadOnly", "setReadOnly"); + put("execute", "execute"); + put("executeBatch", "executeBatch"); + put("executeLargeBatch", "executeLargeBatch"); + put("executeLargeUpdate", "executeLargeUpdate"); + put("executeQuery", "executeQuery"); + put("executeUpdate", "executeUpdate"); + put("executeWithFlags", "executeWithFlags"); + put("getParameterMetaData", "getParameterMetaData"); + }}.newKeySet(); private static final String METHOD_GET_TRANSACTION_ISOLATION = "getTransactionIsolation";