79478468

Date: 2025-03-02 00:12:02
Score: 2
Natty:
Report link

Assuming you are using "BEFORE_COMMIT" transaction phase since you want them to run all in one transaction. When you have a @TransactionalEventListener with phase "BEFORE_COMMIT" implies that there is already an active transaction. You don't need to put another @Transactional annotation. It causes some problems when used with default "AFTER_COMMIT" which is described in detail here, like you mentioned.

Now I have to create a new transaction (REQUIRES_NEW), and if something goes wrong, I can't roll back A entity. Why is this?

No, you don't have to set propagation to "REQUIRES_NEW". You can remove unnecessary @Transactional annotation.

You can also validate if the code is being executed in transaction with the help from: TransactionSynchronizationManager.isActualTransactionActive()

Reasons:
  • RegEx Blacklisted phrase (0.5): Why is this
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @TransactionalEventListener
  • User mentioned (0): @Transactional
  • User mentioned (0): @Transactional
  • Low reputation (1):
Posted by: Abdurrahman Kutlu