Annotation Interface TransactionalEventListener


@Target({METHOD,ANNOTATION_TYPE}) @Retention(RUNTIME) @Documented @EventListener public @interface TransactionalEventListener
An EventListener that is invoked according to a TransactionPhase. This is an annotation-based equivalent of TransactionalApplicationListener.

If the event is not published within an active transaction, the event is discarded unless the fallbackExecution() flag is explicitly set. If a transaction is running, the event is handled according to its TransactionPhase.

Adding @Order to your annotated method allows you to prioritize that listener amongst other listeners running before or after transaction completion.

NOTE: Transactional event listeners only work with thread-bound transactions managed by a PlatformTransactionManager. A reactive transaction managed by a ReactiveTransactionManager uses the Reactor context instead of thread-local variables, so from the perspective of an event listener, there is no compatible active transaction that it can participate in.

WARNING: if the TransactionPhase is set to AFTER_COMMIT (the default), AFTER_ROLLBACK, or AFTER_COMPLETION, the transaction will have been committed or rolled back already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, but changes will not be committed to the transactional resource. See TransactionSynchronization.afterCompletion(int) for details.

Since:
4.2
Author:
Stephane Nicoll, Sam Brannen, Oliver Drotbohm
See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Class<?>[]
    The event classes that this listener handles.
    Spring Expression Language (SpEL) attribute used for making the event handling conditional.
    boolean
    Whether the event should be handled if no transaction is running.
    An optional identifier for the listener, defaulting to the fully-qualified signature of the declaring method (e.g.
    Phase to bind the handling of an event to.
    Class<?>[]
    Alias for classes().