DEV Community

Discussion on: Spring Data — Power of Domain Events

Collapse
kirekov profile image
Semyon Kirekov Author

@cezarcruz
These two approaches have approximately the same performance.

Though there might be a problem if you have too many listeners called on BEFORE_COMMIT phase. As long as they act synchronously, it can impact the transaction running time.

Anyway, it's not the end of the world. You can make your listeners run asynchronously

@Component
public class Listener {
    @TransactionalEventListener(phase = AFTER_COMMIT)
    @Async
    void captureEvent(SomeEvent event) {
        // do stuff
    }
}
Enter fullscreen mode Exit fullscreen mode

But not all listeners should be run asynchronously. For example, email sender is a perfect candidate. Whilst the one that stores archive records is not.