The Real Fullstack

The Invisible Rollback

The method is annotated @Transactional and the payment threw. Why is the unpaid order still in the database?

エンジニア9 分コード課題タイムラインシミュレーター
javaspring-bootspring-data-jpapostgresql

この課題はまだ日本語に翻訳されていません。英語版を表示しています。

ミッション:Make a declined payment leave no order behind, while the audit entry recording the attempt survives.背景 · 学習目標 ▾

Brightcart's checkout saves an order, writes an audit entry and charges the card. When the card is declined, customers still see a “pending” order in their account, and the audit trail has no record of the failure. The code looks correct at first glance.

ミッション: Make a declined payment leave no order behind, while the audit entry recording the attempt survives.

学習目標

  • Explain why Spring's @Transactional doesn't apply to self-invocation.
  • Know which exceptions trigger rollback by default.
  • Use REQUIRES_NEW for records that must outlive a rollback.
シミュレーションシミュレーション値

シミュレーションを実行すると出力が表示されます。

シミュレーションを実行
@Servicepublic class CheckoutService {  public Receipt checkout(Cart cart) throws PaymentDeclinedException {    return placeOrder(cart);                       // this.placeOrder(): the proxy is bypassed  }  @Transactional                                   // never applied for self-invocation  public Receipt placeOrder(Cart cart) throws PaymentDeclinedException {    Order order = orders.save(Order.from(cart));   // committed immediately by the repository    audit.record("checkout", order.id());    payments.charge(order);                        // throws    return Receipt.of(order);  }}

簡略化

One method, one exception, Spring's default proxy mode. AspectJ weaving and reactive transactions behave differently. 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

関連する課題

参考資料