I am studying on the programming in DDD and event source.
I saw one example that when a domain logic was called (e.g. Order.placeOrder()
) it would publish an event (e.g. OrderPlaced
). And the event would be sent to MQ as the event store.
The domain logic (Order.placeOrder()
) should be an atomic API, and it should have @Transactional
annotation if using Spring for the transaction manager.
And now my question is:
How to make sure the DB change and event sending are within the same transaction? i.e. If there any error when committing data into DB, the event should never send to MQ.
I know that there is solution like XA or 2 phase commit to force the DB update and sending MQ messages within the same transaction. But seems it is not widely used nowadays.
If still using Spring
@Transactional
annotation and no XA, is it possible that we do some logic after the transaction is committed successfully? What is the best practice to do that?