What will happen when we use entity.saveAndFlush()
inside method that is annotated as Transactional
in case of a rollback. I understand that we will synchronized changes inside persistence context, but crucial question is how rollback is going to affect those changes.
@Transactional
void foo(){
Order order = orderRepository.fetchById(event.orderId());
order.setBuyer(new Buyer)
orderRepository.saveAndFlush(order);
throw new RuntimeException
}
Will changes flushed to DB reverted in case of RuntimeException
?
What will happen when we use entity.saveAndFlush()
inside method that is annotated as Transactional
in case of a rollback. I understand that we will synchronized changes inside persistence context, but crucial question is how rollback is going to affect those changes.
@Transactional
void foo(){
Order order = orderRepository.fetchById(event.orderId());
order.setBuyer(new Buyer)
orderRepository.saveAndFlush(order);
throw new RuntimeException
}
Will changes flushed to DB reverted in case of RuntimeException
?
- yes. And persitence context should not be reused. – Luca Basso Ricci Commented Dec 6, 2024 at 13:52
2 Answers
Reset to default 1Yes. Transaction will be rolled back.
Yes, the changes flushed to the database via saveAndFlush() will be rolled back if a RuntimeException is thrown, assuming the method is properly annotated with @Transactional. Here's why: Key Points to Understand
Transactional Boundaries: The @Transactional annotation ensures that all operations within the method are part of the same transaction. If any exception that causes a rollback (like a RuntimeException by default) occurs, the transaction is marked for rollback.
Persistence Context and saveAndFlush(): When you call saveAndFlush(), it writes the changes to the database immediately, unlike save() which delays the write until the transaction is committed. However, this write is still part of the same ongoing transaction. The data is not permanently committed to the database yet.
Rollback Behavior: When a rollback is triggered (due to the RuntimeException in your example), the transaction manager undoes all changes made during the transaction. This includes changes that were flushed to the database using saveAndFlush(). The database system ensures that no partial changes are persisted, as all operations are encapsulated in the transaction's lifecycle.
Example Breakdown
Here's your code and its behavior step-by-step:
@Transactional
void foo() {
Order order = orderRepository.fetchById(event.orderId());
order.setBuyer(new Buyer()); // Modify an entity
orderRepository.saveAndFlush(order); // Synchronize changes to DB but within the transaction
throw new RuntimeException(); // Trigger a rollback
}
- The order.setBuyer() modifies the entity in the persistence context.
- saveAndFlush() ensures the changes are synchronized with the database but still part of the transaction.
- When RuntimeException is thrown, the transaction manager rolls back the transaction, undoing the effects of saveAndFlush().