Skip to content

Commit

Permalink
fix: Double-checked locking for logConstructor
Browse files Browse the repository at this point in the history
  • Loading branch information
qingbozhang committed May 2, 2024
1 parent cddff0d commit 55ce51c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/main/java/org/apache/ibatis/logging/LogFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class LogFactory {
public static final String MARKER = "MYBATIS";

private static final ReentrantLock lock = new ReentrantLock();
private static Constructor<? extends Log> logConstructor;
private static volatile Constructor<? extends Log> logConstructor;

static {
tryImplementation(LogFactory::useSlf4jLogging);
Expand Down Expand Up @@ -95,10 +95,14 @@ public static void useNoLogging() {

private static void tryImplementation(Runnable runnable) {
if (logConstructor == null) {
try {
runnable.run();
} catch (Throwable t) {
// ignore
synchronized (LogFactory.class) {
if (logConstructor == null) {
try {
runnable.run();
} catch (Throwable t) {
// ignore
}
}
}
}
}
Expand Down

0 comments on commit 55ce51c

Please sign in to comment.