Book Club Saturdays
The Metropolitan Public Library hosts "Book Club Saturdays," a wildly popular event where 30-40 members arrive at 10 AM to check out their monthly selections. The current system processes each loan individually with auto-commit enabled, creating two critical problems:
- Data inconsistency: Last month, the system crashed while processing a member's four-book checkout. Two books were recorded as loaned, but the other two weren't, causing inventory discrepancies and frustrated patrons.
- Performance bottleneck: Processing each loan as a separate database transaction takes 15-20 minutes, creating long queues and unhappy library staff.
Your mission is to implement batch processing with transaction control.
This exercise is part of the course
Querying a PostgreSQL Database in Java
Exercise instructions
- Set
autoCommittofalseto enable manual transaction control in line 32. - Add multiple loans to the batch in line 43.
- Commit the transaction if all operations are successful in line 49.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
public class BatchLoanProcessor {
private static final String DB_URL = "jdbc:postgresql://localhost:5432/library_db";
private static final String USERNAME = "postgres";
private static final String PASSWORD = "postgres";
public static void main(String[] args) {
Object[][] loanData = {
{1, 1, LocalDate.now(), LocalDate.now().plusDays(14), "borrowed"},
{2, 2, LocalDate.now(), LocalDate.now().plusDays(14), "borrowed"},
{3, 3, LocalDate.now(), LocalDate.now().plusDays(7), "borrowed"},
{4, 1, LocalDate.now(), LocalDate.now().plusDays(14), "borrowed"}
};
try {
boolean success = processBatchLoans(loanData);
if (success) {
System.out.println("All loans were processed successfully.");
} else {
System.out.println("Loan processing failed. Transaction was rolled back.");
}
} catch (SQLException e) {
System.err.println("Database error: " + e.getMessage());
e.printStackTrace();
}
}
public static boolean processBatchLoans(Object[][] loanData) throws SQLException {
String insertSQL = "INSERT INTO loans (book_id, member_id, loan_date, due_date, status) VALUES (?, ?, ?, ?, ?)";
try (Connection conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD)) {
// Set autoCommit to false
conn.____(____);
try (PreparedStatement pstmt = conn.prepareStatement(insertSQL)) {
for (Object[] loan : loanData) {
pstmt.setInt(1, (Integer) loan[0]);
pstmt.setInt(2, (Integer) loan[1]);
pstmt.setDate(3, java.sql.Date.valueOf((LocalDate) loan[2]));
pstmt.setDate(4, java.sql.Date.valueOf((LocalDate) loan[3]));
pstmt.setString(5, (String) loan[4]);
// Add to batch
pstmt.____();
}
int[] results = pstmt.executeBatch();
// Commit the transaction if all operations are successful
conn.____();
return true;
} catch (SQLException e) {
conn.rollback();
System.err.println("Error during batch processing: " + e.getMessage());
return false;
}
}
}
}