简体   繁体   中英

Keycloak custom spi - custom transaction timeout

I am trying to do a sync process via the keycloak ImportSynchronization interface. (wildlfy 18.0.1.Final) it provides the following method to override:

    @Override
    public SynchronizationResult sync(
        final KeycloakSessionFactory sessionFactory,
        final String realmId,
        final UserStorageProviderModel model) {
    ...}

This sync process takes much more time than our default transaction timeout (300 sec = 5 min).

I am aware of this possible opportunity:

            <core-environment node-identifier="${jboss.tx.node.id:1}">
                <process-id>
                    <uuid/>
                </process-id>
            </core-environment>
            <recovery-environment socket-binding="txn-recovery-environment" status-socket-binding="txn-status-manager"/>
            <coordinator-environment statistics-enabled="${wildfly.transactions.statistics-enabled:${wildfly.statistics-enabled:false}}"
                                     default-timeout="${env.TRANSACTION_TIMEOUT_SEC:300}"/>
            <object-store path="tx-object-store" relative-to="jboss.server.data.dir"/>
        </subsystem>

BUT this is not what I want, unfortunately.

I don't want to increase the timeout for all of our processes, just for this one. Meaning I am searching for a custom transaction timeout solution. (something like @TransactionTimeout...)


I've tried the following


@Override
    public SynchronizationResult doCleanup(final KeycloakSessionFactory keycloakSessionFactory, final String realmId)  {
        final SynchronizationResult synchronizationResult = new SynchronizationResult();

        try {

            final RealmModel realmModel = this.getRealmModelInTx(realmId, keycloakSessionFactory);

            Thread.sleep(<more than tx timeout>);

            final int userEntityCountBeforeCleanup = this.getUserCountInTx(realmModel, keycloakSessionFactory);
...}

and in both methods, I created a new keycloakSession and a new transactionManager and closed them like here:

    private CleanupTransactionHandler createSession(final KeycloakSessionFactory  keycloakSessionFactory) {
        KeycloakTransactionManager transactionManager = null;
        final KeycloakSession keycloakSession = keycloakSessionFactory.create();

        log.tracef("KeycloakSession has been created [%s].", keycloakSession.hashCode());
        transactionManager = keycloakSession.getTransactionManager();
        transactionManager.begin();
        log.tracef("KeycloakTransactionManager's transaction has been begun [%s].", transactionManager.hashCode());
        return new CleanupTransactionHandler(keycloakSession,transactionManager);
    }

    private void closeSession(final KeycloakSession keycloakSession) {
        keycloakSession.close();
        log.tracef("KeycloakSession has been closed [%s].", keycloakSession.hashCode());
    }

    private void rollback(final KeycloakTransactionManager keycloakTransactionManager) {
        keycloakTransactionManager.rollback();
        log.tracef("KeycloakTransactionManager's transaction has been rolled back [%s].", keycloakTransactionManager.hashCode());
    }

I can answer my own answer.

There is no possibility to customize the transaction timeout for this process, since ImportSynchronization.sync is called from KeycloakModelUtils.runJobInTransaction , that can only configured via default-timeout as mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM