Search in sources :

Example 1 with DBLockProvider

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

the class JpaMapStorageProviderFactory method update.

private void update(Class<?> modelType, Connection connection, KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession lockSession) -> {
        // TODO locking tables based on modelType: https://github.com/keycloak/keycloak/issues/9388
        DBLockProvider dbLock = session.getProvider(DBLockProvider.class);
        dbLock.waitForLock(DBLockProvider.Namespace.DATABASE);
        try {
            session.getProvider(MapJpaUpdaterProvider.class).update(modelType, connection, config.get("schema"));
        } finally {
            dbLock.releaseLock();
        }
    });
}
Also used : KeycloakSession(org.keycloak.models.KeycloakSession) MapJpaUpdaterProvider(org.keycloak.models.map.storage.jpa.updater.MapJpaUpdaterProvider) DBLockProvider(org.keycloak.models.dblock.DBLockProvider)

Example 2 with DBLockProvider

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

the class QuarkusJpaConnectionProviderFactory method update.

private void update(Connection connection, String schema, KeycloakSession session, JpaUpdaterProvider updater) {
    DBLockManager dbLockManager = new DBLockManager(session);
    DBLockProvider dbLock2 = dbLockManager.getDBLock();
    dbLock2.waitForLock(DBLockProvider.Namespace.DATABASE);
    try {
        updater.update(connection, schema);
    } finally {
        dbLock2.releaseLock();
    }
}
Also used : DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider)

Example 3 with DBLockProvider

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

the class QuarkusJpaConnectionProviderFactory method export.

private void export(Connection connection, String schema, File databaseUpdateFile, KeycloakSession session, JpaUpdaterProvider updater) {
    DBLockManager dbLockManager = new DBLockManager(session);
    DBLockProvider dbLock2 = dbLockManager.getDBLock();
    dbLock2.waitForLock(DBLockProvider.Namespace.DATABASE);
    try {
        updater.export(connection, schema, databaseUpdateFile);
    } finally {
        dbLock2.releaseLock();
    }
}
Also used : DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider)

Example 4 with DBLockProvider

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

the class DBLockBasedCacheInitializer method startLoading.

/**
 * Just coordinator will run this. And there is DB-lock, so the delegate.startLoading() will be permitted just by the single DC
 */
@Override
protected void startLoading() {
    DBLockManager dbLockManager = new DBLockManager(session);
    dbLockManager.checkForcedUnlock();
    DBLockProvider dbLock = dbLockManager.getDBLock();
    dbLock.waitForLock(DBLockProvider.Namespace.OFFLINE_SESSIONS);
    try {
        if (isFinished()) {
            log.infof("Task already finished when DBLock retrieved");
        } else {
            delegate.startLoading();
        }
    } finally {
        dbLock.releaseLock();
    }
}
Also used : DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider)

Example 5 with DBLockProvider

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

the class DBLockTest method simpleNestedLockTest.

@Test
public void simpleNestedLockTest() throws Exception {
    inComittedTransaction(1, (session, i) -> {
        // first session lock DATABASE
        DBLockProvider dbLock1 = new DBLockManager(session).getDBLock();
        dbLock1.waitForLock(DBLockProvider.Namespace.DATABASE);
        try {
            Assert.assertEquals(DBLockProvider.Namespace.DATABASE, dbLock1.getCurrentLock());
            KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionLC2) -> {
                // a second session/dblock-provider can lock another namespace OFFLINE_SESSIONS
                DBLockProvider dbLock2 = new DBLockManager(sessionLC2).getDBLock();
                dbLock2.waitForLock(DBLockProvider.Namespace.OFFLINE_SESSIONS);
                try {
                    // getCurrentLock is local, each provider instance has one
                    Assert.assertEquals(DBLockProvider.Namespace.OFFLINE_SESSIONS, dbLock2.getCurrentLock());
                } finally {
                    dbLock2.releaseLock();
                }
                Assert.assertNull(dbLock2.getCurrentLock());
            });
        } finally {
            dbLock1.releaseLock();
        }
        Assert.assertNull(dbLock1.getCurrentLock());
        return null;
    });
}
Also used : KeycloakSession(org.keycloak.models.KeycloakSession) DBLockManager(org.keycloak.models.dblock.DBLockManager) DBLockProvider(org.keycloak.models.dblock.DBLockProvider) Test(org.junit.Test)

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