Spring Boot services · Chapter 21 of 41

Cascading and transactions

By the end of this chapter you can save and delete a parent with its children in one call, and make several writes happen all together or not at all. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Data
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

The 6 am rush at Bengaluru station, and Ravi is not happy: "The app sold three samosas we didn't have. And when it said out of stock, it had already taken the chai off the stock anyway. Now the count says 7 chai and the shelf has 10."

Placing an order is several writes: the order, its lines, and one stock row per item. With what you know so far, each repository call is a write of its own: the lines need saving one by one, and when the third item fails, the first two have already been taken for good.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Order.java: cascade saves and deletes to the lines

    Make saving a new order save the new lines it holds, and deleting an order delete its lines: an order with two lines from addLine, saved with orders.save, gives 2 rows in order_lines, and orders.deleteById then leaves 0 orders and 0 lines.

  2. Task 2

    Order.java: delete a line taken off its order

    Make a line removed from an order's lines list deleted from the database: with Masala chai and Bun maska saved for an order, removeLine("Bun maska") inside a transaction leaves only Masala chai in order_lines.

  3. Task 3

    OrderService.java: place an order and take the stock

    Write place(station, items) so it makes a new order for the station with one line per LineRequest, at that item's price in the stock, takes each quantity off the stock with Stock.take, saves the order with its lines and returns the saved order (its id is not null).

    place("SBC", Masala chai x 2, Bun maska x 1) saves 1 order and 2 lines with a total of 130, and leaves Masala chai at 8 and Bun maska at 4 in the database. The stock change must be written, so the method runs in one transaction. Needs task 1.

  4. Task 4

    OrderService.java: all or nothing when stock runs out

    Make place change nothing when any item runs out: an item with no stock row, or not enough of it, throws OutOfStockException, and afterwards there is no order, no line, and the stock already taken for earlier items is back.

    place("CSMT", Masala chai x 3, Bun maska x 1, Samosa x 1) with 0 Samosa throws OutOfStockException, and leaves Masala chai at 10, Bun maska at 5, 0 orders and 0 lines.

  5. Task 5 · interview question

    Interview: roll back a declined payment, a checked exception

    Make placePrepaid keep nothing when it throws PaymentDeclinedException: Masala chai x 2 (bill 80) paid 50 throws PaymentDeclinedException, and Masala chai stays at 10 with 0 orders. Paid 80, the order is kept: total 80, Masala chai at 8, 1 order.

    Be ready to explain why Spring commits on a checked exception, and how to make it roll back.

What you will learn

  • Cascades: an operation on a parent passed on to its children.
  • Orphan removal: a child taken off its parent's list is deleted.
  • @Transactional: every write in a method commits together, or is rolled back together.
  • Which exceptions roll back: and why a checked one does not (the interview question).

More in Data

Build "Cascading and transactions" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.