1
JavaAdvanced#jdbc
Disable auto-commit on the Connection, execute multiple statements, then explicitly call commit() on success or rollback() on failure, ensuring atomicity across multiple SQL operations.
conn.setAutoCommit(false);
try {
stmt.executeUpdate("UPDATE accounts SET balance = balance - 100 WHERE id = 1");
stmt.executeUpdate("UPDATE accounts SET balance = balance + 100 WHERE id = 2");
conn.commit();
} catch (SQLException e) { conn.rollback(); }