最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Java, spring, @Transactional - Stack Overflow

programmeradmin18浏览0评论

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?

Share Improve this question edited Dec 6, 2024 at 11:40 talex 20.2k3 gold badges33 silver badges68 bronze badges asked Dec 6, 2024 at 11:18 mihumihu 655 bronze badges 1
  • yes. And persitence context should not be reused. – Luca Basso Ricci Commented Dec 6, 2024 at 13:52
Add a comment  | 

2 Answers 2

Reset to default 1

Yes. 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().
发布评论

评论列表(0)

  1. 暂无评论