1. Learn
  2. /
  3. Courses
  4. /
  5. Querying a PostgreSQL Database in Java

Connected

Exercise

Returning a book

You are a developer for the Metropolitan Public Library's management system. Last week, a critical bug was discovered: when the system crashed during a book return, the loan was marked as "returned" but the book remained "checked-out" in the inventory. This left the book unavailable for weeks until staff manually corrected the database.

When a book is returned, three operations must succeed together or fail together:

  1. Update the loan record to "returned" status.
  2. Change the book's availability from "checked-out" to "available".
  3. Record a fine if the book is returned late.

Your task is to implement proper transaction control to ensure data consistency.

Instructions

100 XP
  • Set autoCommit to false at the beginning in line 26.
  • Commit the transaction if all operations succeed in line 59.
  • If any operation fails, roll back the transaction in line 66.