Package org.keycloak.connections.jpa
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 OFFMSSQLAsyncCommitIntegrator—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 -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidapplyAsyncCommit(Connection connection) Apply the database-specific mechanism to make the current transaction's commit asynchronous.protected abstract booleanisEnabled(org.hibernate.engine.spi.SessionFactoryImplementor sf) Whether asynchronous commit should be enabled for this database.booleanonPreDelete(org.hibernate.event.spi.PreDeleteEvent event) booleanonPreInsert(org.hibernate.event.spi.PreInsertEvent event) booleanonPreUpdate(org.hibernate.event.spi.PreUpdateEvent event) static voidregisterListeners(jakarta.persistence.EntityManagerFactory emf, boolean xaEnabled) Registers asynchronous commit listeners on the givenEntityManagerFactoryif the underlying database supports it.protected booleanWhether this implementation is compatible with XA datasources.
-
Constructor Details
-
AsyncCommitIntegrator
public AsyncCommitIntegrator()
-
-
Method Details
-
registerListeners
public static void registerListeners(jakarta.persistence.EntityManagerFactory emf, boolean xaEnabled) Registers asynchronous commit listeners on the givenEntityManagerFactoryif the underlying database supports it. No-op for unsupported databases.- Parameters:
xaEnabled- whether the datasource uses XA transactions. Implementations that issue a rawCOMMIT(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 rawCOMMITinside the managed transaction must returnfalse— XA coordinators do not allow explicit commit on enlisted connections (Oracle raises ORA-02089, SQL Server rejects commits outside the XA coordinator).Defaults to
truefor 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
Apply the database-specific mechanism to make the current transaction's commit asynchronous. Called viadoWorkjust before Hibernate issues the JDBC commit.- Throws:
SQLException
-
onPreInsert
public boolean onPreInsert(org.hibernate.event.spi.PreInsertEvent event) - Specified by:
onPreInsertin interfaceorg.hibernate.event.spi.PreInsertEventListener
-
onPreUpdate
public boolean onPreUpdate(org.hibernate.event.spi.PreUpdateEvent event) - Specified by:
onPreUpdatein interfaceorg.hibernate.event.spi.PreUpdateEventListener
-
onPreDelete
public boolean onPreDelete(org.hibernate.event.spi.PreDeleteEvent event) - Specified by:
onPreDeletein interfaceorg.hibernate.event.spi.PreDeleteEventListener
-