diff --git a/model/map/src/main/java/org/keycloak/models/map/common/AbstractMapProviderFactory.java b/model/map/src/main/java/org/keycloak/models/map/common/AbstractMapProviderFactory.java index 06abc2f617..56fb9385ce 100644 --- a/model/map/src/main/java/org/keycloak/models/map/common/AbstractMapProviderFactory.java +++ b/model/map/src/main/java/org/keycloak/models/map/common/AbstractMapProviderFactory.java @@ -108,6 +108,7 @@ public abstract class AbstractMapProviderFactory getStorage(KeycloakSession session) { ProviderFactory storageProviderFactory = getProviderFactoryOrComponentFactory(session, storageConfigScope); final MapStorageProvider factory = storageProviderFactory.create(session); + session.enlistForClose(factory); return factory.getStorage(modelType); } diff --git a/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java b/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java index ee88f8e323..4894438f77 100644 --- a/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java +++ b/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java @@ -127,6 +127,11 @@ public class DefaultKeycloakSession implements KeycloakSession { @Override public void enlistForClose(Provider provider) { + for (Provider p : closable) { + if (p == provider) { // Do not add the same provider twice + return; + } + } closable.add(provider); }