KEYCLOAK-17870 Fix ConcurrentModificationException upon liquibase initialization

This commit is contained in:
Hynek Mlnarik 2021-04-26 10:43:57 +02:00 committed by Hynek Mlnařík
parent df4f88a0c8
commit 3e91e6f966

View file

@ -48,6 +48,7 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.KeycloakSessionFactory;
import java.sql.Connection; import java.sql.Connection;
import java.util.concurrent.atomic.AtomicBoolean;
import liquibase.changelog.ChangeLogHistoryServiceFactory; import liquibase.changelog.ChangeLogHistoryServiceFactory;
/** /**
@ -61,15 +62,18 @@ public class DefaultLiquibaseConnectionProvider implements LiquibaseConnectionPr
private int indexCreationThreshold; private int indexCreationThreshold;
private volatile boolean initialized = false; private static final AtomicBoolean INITIALIZATION = new AtomicBoolean(false);
@Override @Override
public LiquibaseConnectionProvider create(KeycloakSession session) { public LiquibaseConnectionProvider create(KeycloakSession session) {
if (!initialized) { if (! INITIALIZATION.get()) {
synchronized (this) { // We need critical section synchronized on some static final field, otherwise
if (!initialized) { // e.g. several Undertows or parallel model tests could attempt initializing Liquibase
// in the same JVM at the same time which leads to concurrency failures
synchronized (INITIALIZATION) {
if (! INITIALIZATION.get()) {
baseLiquibaseInitialization(); baseLiquibaseInitialization();
initialized = true; INITIALIZATION.set(true);
} }
} }
} }