Annotation Interface TransactionalEventListener
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
TransactionSynchronizationof that transaction andpublishEventreturns without invoking the method. The method is invoked later, at the phase it declares, by defaultTransactionalEventListener.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. WithREQUIRES_NEWit 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.TransactionalEventListenerlogger).
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe phase at which a transactional event listener applies. -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionA qualifier value for the specified transaction.
-
Element Details
-
value
- Returns:
- The transaction phase this listener applies to.
- Default:
AFTER_COMMIT
-
transactionManager
String transactionManagerA 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
SynchronousTransactionManagerbean definition.- Returns:
- The transaction manager
- Since:
- 3.5.0
- Default:
""
-