Annotation Interface TransactionalEventListener


@Target({METHOD,ANNOTATION_TYPE}) @Retention(RUNTIME) @Documented @Adapter(io.micronaut.context.event.ApplicationEventListener.class) @Indexed(io.micronaut.context.event.ApplicationEventListener.class) public @interface TransactionalEventListener
Declares a method as an application event listener that runs at a specific phase of the transaction in which the event was published, instead of immediately. The method must take the event as its single parameter; Micronaut turns it into an ApplicationEventListener for that event type (this annotation is an Adapter).

When an event is published with ApplicationEventPublisher.publishEvent(Object), an ordinary listener is invoked straight away, while the transaction is still open and its outcome unknown. A transactional event listener instead behaves as follows:

  • If a transaction is active in the publishing context, the call to the listener is deferred: it is registered as a TransactionSynchronization of that transaction and publishEvent returns without invoking the method. The method is invoked later, at the phase it declares, by default TransactionalEventListener.TransactionPhase.AFTER_COMMIT, so that it only reacts to data that was actually committed.
  • If the publishing code joined an outer transaction (for example with propagation REQUIRED), the listener is bound to that outer transaction and runs when the outer transaction completes. With REQUIRES_NEW it runs when the new, inner transaction completes.
  • If no transaction is active, the event is discarded for this listener: the method is not invoked (a debug message is logged by the io.micronaut.transaction.annotation.TransactionalEventListener logger).

The listener runs synchronously on the thread that commits or rolls back the transaction, which is normally the thread that published the event. TransactionalEventListener.TransactionPhase.BEFORE_COMMIT listeners run while the transaction is still open: they can use it, and an exception they throw rolls it back and is propagated to the caller. The after-completion phases run once the transaction has been committed or rolled back, so their exceptions cannot change the outcome; they are propagated to the caller of the transactional method. To write to the database from an after-completion listener, start a new transaction, for example by annotating the listener with @Transactional(Transactional.TxType.REQUIRES_NEW).

The transaction is looked up using the synchronous transaction manager, selected with transactionManager(). Transactions of reactive transaction managers are not supported.

Since:
1.0.0
Author:
graemerocher
See Also:
  • Element Details

    • value

      Returns:
      The transaction phase this listener applies to.
      Default:
      AFTER_COMMIT
    • transactionManager

      String transactionManager
      A qualifier value for the specified transaction.

      May be used to determine the target transaction manager, matching the qualifier value (or the bean name) of a specific SynchronousTransactionManager bean definition.

      Returns:
      The transaction manager
      Since:
      3.5.0
      Default:
      ""