简体   繁体   English

Keycloak custom spi - 自定义事务超时

[英]Keycloak custom spi - custom transaction timeout

I am trying to do a sync process via the keycloak ImportSynchronization interface.我正在尝试通过keycloak ImportSynchronization接口执行同步过程。 (wildlfy 18.0.1.Final) it provides the following method to override: (wildlfy 18.0.1.Final) 它提供了以下方法来覆盖:

    @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).此同步过程比我们的默认事务超时(300 秒 = 5 分钟)花费更多的时间

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...) (类似于@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:在这两种方法中,我创建了一个新的 keycloakSession 和一个新的 transactionManager并像这里一样关闭它们:

    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.无法为此过程自定义事务超时,因为 ImportSynchronization.sync 是从KeycloakModelUtils.runJobInTransaction调用的,只能通过如上所述的默认超时进行配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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