0
# Transactional Events
1
2
Spring's transactional event support coordinates event processing with transaction lifecycle for reliable event handling.
3
4
## @TransactionalEventListener
5
6
```java { .api }
7
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
8
@Retention(RetentionPolicy.RUNTIME)
9
@Documented
10
@EventListener
11
public @interface TransactionalEventListener {
12
TransactionPhase phase() default TransactionPhase.AFTER_COMMIT;
13
String condition() default "";
14
boolean fallbackExecution() default false;
15
String id() default "";
16
}
17
```
18
19
## TransactionPhase
20
21
```java { .api }
22
public enum TransactionPhase {
23
BEFORE_COMMIT, AFTER_COMMIT, AFTER_ROLLBACK, AFTER_COMPLETION
24
}
25
```
26
27
## Usage Examples
28
29
```java
30
@Component
31
public class OrderEventListener {
32
33
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
34
public void handleOrderCreated(OrderCreatedEvent event) {
35
// Send email after transaction commits successfully
36
emailService.sendOrderConfirmation(event.getOrder());
37
}
38
39
@TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK)
40
public void handleOrderFailed(OrderCreatedEvent event) {
41
// Clean up resources after transaction rollback
42
cleanupService.cleanupOrder(event.getOrder());
43
}
44
45
@TransactionalEventListener(
46
phase = TransactionPhase.AFTER_COMMIT,
47
condition = "#event.order.amount.compareTo(new java.math.BigDecimal('1000')) > 0"
48
)
49
public void handleLargeOrder(OrderCreatedEvent event) {
50
// Special handling for large orders
51
auditService.auditLargeOrder(event.getOrder());
52
}
53
}
54
```