Hi,

In my custom RealmResourceProviderFactory, I do roughly the following:

    @Override
    public void postInit(KeycloakSessionFactory factory) {
        KeycloakModelUtils.runJobInTransaction(factory, (KeycloakSession session) -> {
            List<RealmModel> realms = session.realms().getRealms();
            ...
	});
    });

Full code here: https://github.com/dteleguin/custom-admin-roles

Everything worked fine with 2.1.x, but after upgrade to 2.2.x startup fails in roughly about 50% cases:

Caused by: java.sql.SQLException: IJ031017: You cannot set autocommit during a managed transaction
	at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.setJdbcAutoCommit(BaseWrapperManagedConnection.java:994)
	at org.jboss.jca.adapters.jdbc.WrappedConnection.setAutoCommit(WrappedConnection.java:787)
	at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.begin(AbstractLogicalConnectionImplementor.java:67)
	at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.begin(LogicalConnectionManagedImpl.java:238)
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.begin(JdbcResourceLocalTransactionCoordinatorImpl.java:214)
	at org.hibernate.engine.transaction.internal.TransactionImpl.begin(TransactionImpl.java:52)
	at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1512)
	at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:45)

Full stacktrace here: http://pastebin.com/ETtPqXQk

In the other half of cases, everything goes fine just like before, so it's a kind of heisenbug. Any ideas? Could it be some concurrency issue when my code is executed in parallel with some other DB-related code? could it be JTA related?

Dmitry