Spring Boot services · Chapter 33 of 41
Transactions and locking
By the end of this chapter you can decide whether a transaction joins another or runs on its own, mark one read-only, and stop two people from overwriting each other's changes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Production
- 6 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 SBC counter sold three masala chai and two bun maska with one bun maska left. The order failed, yet the chai were gone from stock, and the audit log Ravi reads every evening had no row about it. The same week, Ravi and Anjali restocked one item from two screens, and Anjali's number silently replaced his.
Chapter 21's @Transactional fixes the first bug, but an audit row written inside the sale's transaction is rolled back with it. And no transaction helps two managers who each load a row and save it a minute later: each save is its own short transaction, and the last one wins.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
StockService.java: make a sale all or nothing
Write StockService.sell(lines) so it takes each line's quantity off that item's stock row, loading each row with stocks.lockByItem(item). When a line asks for more than is left, record "Out of stock: <item>" with audit.record and throw an OutOfStockException for that item, and put back everything the sale already took.
With Masala chai 10, Bun maska 1 and Cutting chai 30: sell(Masala chai x 2, Cutting chai x 5) leaves 8 and 25 in the database; then sell(Masala chai x 3, Bun maska x 2) throws OutOfStockException and leaves Masala chai at 8 and Bun maska at 1. Run the method in one transaction.
- Task 2 · interview question
Interview: keep the audit row even when the sale rolls back
Make AuditLog.record (in AuditEntry.java) keep its row even when the caller's transaction rolls back: after sell(Masala chai x 3, Bun maska x 2) throws OutOfStockException, the audit table holds exactly one row, "Out of stock: Bun maska", while Masala chai is back at 10. Needs task 1.
Be ready to explain what propagation is, and why this only works because record lives in another bean.
- Task 3
StockService.java: a read-only stock report, lowest first
Write StockService.report() so it returns every stock row, lowest quantity first (Bun maska 1, Masala chai 10, Cutting chai 30), and runs in a read-only transaction: a transactional annotation with read-only switched on, on the method.
- Task 4
Stock.java: refuse an update from a stale copy
Make Stock's version field the optimistic-locking version: with two copies of the Masala chai row loaded, saving the first with quantity 20 works and raises the row's version from 0 to 1, and saving the second with quantity 5 afterwards throws an OptimisticLockingFailureException and leaves 20 in the database.
- Task 5
StockController.java: answer 409 for a restock from an old version
Write StockService.restock(id, quantity, version) so it sets the row's quantity and returns the row, in a transaction so the change is written, but throws ObjectOptimisticLockingFailureException(Stock.class, id) when version is not the row's current version. Then make PUT /stock/{id} answer 409 Conflict with a ProblemDetail (its status field is 409) for that exception, instead of 500.
PUT /stock/{id} with {"quantity": 12, "version": 0} on Bun maska answers 200 with quantity 12 and version 1; the same body again (still version 0) answers 409 and leaves 12. Needs task 4.
- Task 6
Stock.java: lock the stock row while a sale reads it
Make StockRepository.lockByItem(item) read the row with a pessimistic write lock (SELECT ... FOR UPDATE), so another transaction that wants the row waits until this one ends. Inside a transaction, lockByItem("Cutting chai") still returns the row with 30.
What you will learn
- Propagation: whether a transactional method joins the caller's transaction or starts its own.
- Read-only transactions: for reports, and what Hibernate skips.
- Optimistic locking: a version number that refuses a stale update.
- Pessimistic locking: SELECT ... FOR UPDATE, so a second sale waits.
More in Production
Build "Transactions and locking" 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.