Search in sources :

Example 6 with DBLockProvider

use of org.keycloak.models.dblock.DBLockProvider in project keycloak by keycloak.

the class DBLockTest method nestedTwoLevelLock.

private void nestedTwoLevelLock(KeycloakSession session, DBLockProvider.Namespace lockTop, DBLockProvider.Namespace lockInner, Semaphore semaphore) {
    DBLockProvider dbLock = new DBLockManager(session).getDBLock();
    dbLock.waitForLock(lockTop);
    try {
        // create a new session to call the lock method with the inner namespace
        KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), sessionInner -> lock(sessionInner, lockInner, semaphore));
    } finally {
        dbLock.releaseLock();
    }
}
Also used : DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider)

Example 7 with DBLockProvider

use of org.keycloak.models.dblock.DBLockProvider in project keycloak by keycloak.

the class DBLockTest method simpleLockTest.

@Test
public void simpleLockTest() throws Exception {
    inComittedTransaction(1, (session, i) -> {
        DBLockProvider dbLock = new DBLockManager(session).getDBLock();
        dbLock.waitForLock(DBLockProvider.Namespace.DATABASE);
        try {
            Assert.assertEquals(DBLockProvider.Namespace.DATABASE, dbLock.getCurrentLock());
        } finally {
            dbLock.releaseLock();
        }
        Assert.assertNull(dbLock.getCurrentLock());
        return null;
    });
}
Also used : DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider) Test(org.junit.Test)

Example 8 with DBLockProvider

use of org.keycloak.models.dblock.DBLockProvider in project keycloak by keycloak.

the class DBLockTest method lock.

private void lock(KeycloakSession session, DBLockProvider.Namespace lock, Semaphore semaphore) {
    DBLockProvider dbLock = new DBLockManager(session).getDBLock();
    dbLock.waitForLock(lock);
    try {
        semaphore.increase();
        Thread.sleep(SLEEP_TIME_MILLIS);
        semaphore.decrease();
    } catch (InterruptedException ie) {
        throw new RuntimeException(ie);
    } finally {
        dbLock.releaseLock();
    }
}
Also used : DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider)

Example 9 with DBLockProvider

use of org.keycloak.models.dblock.DBLockProvider in project keycloak by keycloak.

the class KeycloakApplication method startup.

protected void startup() {
    KeycloakApplication.sessionFactory = createSessionFactory();
    ExportImportManager[] exportImportManager = new ExportImportManager[1];
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            DBLockManager dbLockManager = new DBLockManager(session);
            dbLockManager.checkForcedUnlock();
            DBLockProvider dbLock = dbLockManager.getDBLock();
            dbLock.waitForLock(DBLockProvider.Namespace.KEYCLOAK_BOOT);
            try {
                exportImportManager[0] = bootstrap();
            } finally {
                dbLock.releaseLock();
            }
        }
    });
    if (exportImportManager[0].isRunExport()) {
        exportImportManager[0].runExport();
    }
    KeycloakModelUtils.runJobInTransaction(sessionFactory, new KeycloakSessionTask() {

        @Override
        public void run(KeycloakSession session) {
            boolean shouldBootstrapAdmin = new ApplianceBootstrap(session).isNoMasterUser();
            BOOTSTRAP_ADMIN_USER.set(shouldBootstrapAdmin);
        }
    });
    sessionFactory.publish(new PostMigrationEvent());
    setupScheduledTasks(sessionFactory);
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) KeycloakSessionTask(org.keycloak.models.KeycloakSessionTask) KeycloakSession(org.keycloak.models.KeycloakSession) ExportImportManager(org.keycloak.exportimport.ExportImportManager) DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider) PostMigrationEvent(org.keycloak.models.utils.PostMigrationEvent)

Example 10 with DBLockProvider

use of org.keycloak.models.dblock.DBLockProvider in project keycloak by keycloak.

the class QuarkusJpaConnectionProviderFactory method initSchema.

private void initSchema(KeycloakSession session) {
    ExportImportManager exportImportManager = new ExportImportManager(session);
    /*
         * Migrate model is executed just in case following providers are "jpa".
         * In Map Storage, there is an assumption that migrateModel is not needed.
         */
    if ((Config.getProvider("realm") == null || "jpa".equals(Config.getProvider("realm"))) && (Config.getProvider("client") == null || "jpa".equals(Config.getProvider("client"))) && (Config.getProvider("clientScope") == null || "jpa".equals(Config.getProvider("clientScope")))) {
        logger.debug("Calling migrateModel");
        migrateModel(session);
    }
    DBLockManager dbLockManager = new DBLockManager(session);
    dbLockManager.checkForcedUnlock();
    DBLockProvider dbLock = dbLockManager.getDBLock();
    dbLock.waitForLock(DBLockProvider.Namespace.KEYCLOAK_BOOT);
    try {
        createMasterRealm(exportImportManager);
    } finally {
        dbLock.releaseLock();
    }
    if (exportImportManager.isRunExport()) {
        exportImportManager.runExport();
        Quarkus.asyncExit();
    }
}
Also used : ExportImportManager(org.keycloak.exportimport.ExportImportManager) DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider)

Aggregations

DBLockProvider (org.keycloak.models.dblock.DBLockProvider)10 DBLockManager (org.keycloak.models.dblock.DBLockManager)9 KeycloakSession (org.keycloak.models.KeycloakSession)3 Test (org.junit.Test)2 ExportImportManager (org.keycloak.exportimport.ExportImportManager)2 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)1 MapJpaUpdaterProvider (org.keycloak.models.map.storage.jpa.updater.MapJpaUpdaterProvider)1 PostMigrationEvent (org.keycloak.models.utils.PostMigrationEvent)1 ApplianceBootstrap (org.keycloak.services.managers.ApplianceBootstrap)1