0%
June 28, 2025

Two kinds of Side Effects in JPA

event-driven

jpa

springboot

When Atomic Behaviour Needed

  • Transactional context will be held to the following function scope
  • When any step here failed, the main transaction will be failed as well
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
fun somethingHappenOn(event: SomeEvent)

When It can Fail Independently

  • The following will be executed only when the transaction is committed
  • Not only that, the @Transactional annotation provides next transactional context
  • When we don't need any transaction (like sending email to the subscribed users), we can omit @Transactional
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT)
@Transactional(propagation = Propagation.REQUIRES_NEW)
fun somethingHappenOn(event: SomeEvent)