Skip to content

Commit

Permalink
refactor: ThreadLocal 초기화
Browse files Browse the repository at this point in the history
  • Loading branch information
Ohjintaek committed Oct 15, 2023
1 parent d0f339d commit 7aaacbd
Showing 1 changed file with 2 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,24 @@
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public abstract class TransactionSynchronizationManager {

private static final ThreadLocal<Map<DataSource, Connection>> resources = new ThreadLocal<>();
private static final ThreadLocal<Map<DataSource, Connection>> resources = ThreadLocal.withInitial(HashMap::new);

private TransactionSynchronizationManager() {
}

public static Connection getResource(DataSource key) {
Map<DataSource, Connection> connectionMap = resources.get();
if (Objects.isNull(connectionMap)) {
if (!connectionMap.containsKey(key)) {
return null;
}
return connectionMap.get(key);
}

public static void bindResource(DataSource key, Connection value) {
Map<DataSource, Connection> connectionMap = resources.get();
if (Objects.isNull(connectionMap)) {
connectionMap = new HashMap<>();
}
connectionMap.put(key, value);
resources.set(connectionMap);
}
Expand Down

0 comments on commit 7aaacbd

Please sign in to comment.