Search in sources :

Example 1 with DBLockManager

use of org.keycloak.models.dblock.DBLockManager 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 2 with DBLockManager

use of org.keycloak.models.dblock.DBLockManager 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 3 with DBLockManager

use of org.keycloak.models.dblock.DBLockManager 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 4 with DBLockManager

use of org.keycloak.models.dblock.DBLockManager 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)

Example 5 with DBLockManager

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

the class DBLockTest method before.

@Before
public void before() throws Exception {
    inComittedTransaction(1, (session, i) -> {
        // Set timeouts for testing
        DBLockManager lockManager = new DBLockManager(session);
        DBLockProviderFactory lockFactory = lockManager.getDBLockFactory();
        lockFactory.setTimeouts(LOCK_RECHECK_MILLIS, LOCK_TIMEOUT_MILLIS);
        // Drop lock table, just to simulate racing threads for create lock table and insert lock record into it.
        lockManager.getDBLock().destroyLockInfo();
        return null;
    });
}
Also used : DBLockProviderFactory(org.keycloak.models.dblock.DBLockProviderFactory) DBLockManager(org.keycloak.models.dblock.DBLockManager) Before(org.junit.Before)

Aggregations

DBLockManager (org.keycloak.models.dblock.DBLockManager)10 DBLockProvider (org.keycloak.models.dblock.DBLockProvider)9 Test (org.junit.Test)2 ExportImportManager (org.keycloak.exportimport.ExportImportManager)2 KeycloakSession (org.keycloak.models.KeycloakSession)2 Before (org.junit.Before)1 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)1 DBLockProviderFactory (org.keycloak.models.dblock.DBLockProviderFactory)1 PostMigrationEvent (org.keycloak.models.utils.PostMigrationEvent)1 ApplianceBootstrap (org.keycloak.services.managers.ApplianceBootstrap)1