diff --git a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java index 1cb646294f..550d691805 100644 --- a/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java +++ b/jdbc/src/main/java/org/springframework/transaction/support/TransactionSynchronizationManager.java @@ -4,18 +4,17 @@ import java.sql.Connection; import java.util.HashMap; import java.util.Map; -import java.util.Objects; public abstract class TransactionSynchronizationManager { - private static final ThreadLocal> resources = new ThreadLocal<>(); + private static final ThreadLocal> resources = ThreadLocal.withInitial(HashMap::new); private TransactionSynchronizationManager() { } public static Connection getResource(DataSource key) { Map connectionMap = resources.get(); - if (Objects.isNull(connectionMap)) { + if (!connectionMap.containsKey(key)) { return null; } return connectionMap.get(key); @@ -23,9 +22,6 @@ public static Connection getResource(DataSource key) { public static void bindResource(DataSource key, Connection value) { Map connectionMap = resources.get(); - if (Objects.isNull(connectionMap)) { - connectionMap = new HashMap<>(); - } connectionMap.put(key, value); resources.set(connectionMap); }