Lesson 39: Transactions & ACID in JDBC

09 Aug 2026 1 min Swarnil Singhai

A transaction groups multiple statements so they all succeed or all fail together — critical whenever money or state must stay consistent.

Real-time example

A bank transfer must debit one account and credit another atomically — if the credit fails, the debit must roll back too.

conn.setAutoCommit(false);
try {
    debit(conn, "acc-1", 500);
    credit(conn, "acc-2", 500);
    conn.commit();
} catch (SQLException e) {
    conn.rollback();
    throw e;
}

What's happening

With autoCommit(false), nothing is permanent until commit() — an exception before that triggers rollback(), undoing both statements.

This is where ACID (Atomicity, Consistency, Isolation, Durability) stops being theory and becomes the difference between correct and broken banking code.

Advertisement