DEV Community

Discussion on: Spring Data — Power of Domain Events

kirekov profile image
Semyon Kirekov Author

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

public class Listener {
    @TransactionalEventListener(phase = AFTER_COMMIT)
    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.