Class AsyncCommitIntegrator

java.lang.Object
org.keycloak.connections.jpa.AsyncCommitIntegrator
All Implemented Interfaces:
org.hibernate.event.spi.PreDeleteEventListener, org.hibernate.event.spi.PreInsertEventListener, org.hibernate.event.spi.PreUpdateEventListener

public abstract class AsyncCommitIntegrator extends Object implements org.hibernate.event.spi.PreInsertEventListener, org.hibernate.event.spi.PreUpdateEventListener, org.hibernate.event.spi.PreDeleteEventListener
Hibernate event listener that enables asynchronous commit for transactions that only modify entities implementing AsynchronousCommitAllowed.

Database-specific subclasses implement the actual mechanism:

  • PostgreSQLAsyncCommitIntegrator — SET LOCAL synchronous_commit TO OFF
  • MSSQLAsyncCommitIntegrator — COMMIT TRAN WITH (DELAYED_DURABILITY = ON)
  • OracleAsyncCommitIntegrator — COMMIT WRITE BATCH NOWAIT

MySQL and MariaDB are not supported: their equivalent setting innodb_flush_log_at_trx_commit is session-scoped, not transaction-scoped. Setting it before commit would leave the relaxed durability on the pooled connection, silently affecting subsequent unrelated transactions if the reset fails.

On unsupported databases, registerListeners(EntityManagerFactory, boolean) is a no-op.

Author:
Alexander Schwartz
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract void
    Apply the database-specific mechanism to make the current transaction's commit asynchronous.
    protected abstract boolean
    isEnabled(org.hibernate.engine.spi.SessionFactoryImplementor sf)
    Whether asynchronous commit should be enabled for this database.
    boolean
    onPreDelete(org.hibernate.event.spi.PreDeleteEvent event)
     
    boolean
    onPreInsert(org.hibernate.event.spi.PreInsertEvent event)
     
    boolean
    onPreUpdate(org.hibernate.event.spi.PreUpdateEvent event)
     
    static void
    registerListeners(jakarta.persistence.EntityManagerFactory emf, boolean xaEnabled)
    Registers asynchronous commit listeners on the given EntityManagerFactory if the underlying database supports it.
    protected boolean
    Whether this implementation is compatible with XA datasources.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AsyncCommitIntegrator

      public AsyncCommitIntegrator()
  • Method Details

    • registerListeners

      public static void registerListeners(jakarta.persistence.EntityManagerFactory emf, boolean xaEnabled)
      Registers asynchronous commit listeners on the given EntityManagerFactory if the underlying database supports it. No-op for unsupported databases.
      Parameters:
      xaEnabled - whether the datasource uses XA transactions. Implementations that issue a raw COMMIT (SQL Server, Oracle) are incompatible with XA because the XA coordinator owns the transaction boundaries.
    • supportsXa

      protected boolean supportsXa()
      Whether this implementation is compatible with XA datasources. Implementations that issue a raw COMMIT inside the managed transaction must return false — XA coordinators do not allow explicit commit on enlisted connections (Oracle raises ORA-02089, SQL Server rejects commits outside the XA coordinator).

      Defaults to true for implementations like PostgreSQL that only set session variables.

    • isEnabled

      protected abstract boolean isEnabled(org.hibernate.engine.spi.SessionFactoryImplementor sf)
      Whether asynchronous commit should be enabled for this database. Called once at startup to perform database-specific validation.
    • applyAsyncCommit

      protected abstract void applyAsyncCommit(Connection connection) throws SQLException
      Apply the database-specific mechanism to make the current transaction's commit asynchronous. Called via doWork just before Hibernate issues the JDBC commit.
      Throws:
      SQLException
    • onPreInsert

      public boolean onPreInsert(org.hibernate.event.spi.PreInsertEvent event)
      Specified by:
      onPreInsert in interface org.hibernate.event.spi.PreInsertEventListener
    • onPreUpdate

      public boolean onPreUpdate(org.hibernate.event.spi.PreUpdateEvent event)
      Specified by:
      onPreUpdate in interface org.hibernate.event.spi.PreUpdateEventListener
    • onPreDelete

      public boolean onPreDelete(org.hibernate.event.spi.PreDeleteEvent event)
      Specified by:
      onPreDelete in interface org.hibernate.event.spi.PreDeleteEventListener