Transactions
A transaction groups SQL statements so they all commit or all roll back. You start one, work, then commit or rollback. Autocommit mode is one statement per transaction.
Detailed Explanation
Two updates that must stay together (debit A, credit B) need a transaction. If the process dies after the debit, rollback restores A. ACID is the textbook: atomicity, consistency, isolation, durability. Isolation is the part you configure; the others are why you use a relational database.
JDBC: connection.setAutoCommit(false), work, commit(), and rollback() in catch. Spring: @Transactional on a service method starts a transaction, binds the connection to the thread, and commits on success. Self-invocation skips the proxy; the annotation does not run.
Checked exceptions in Spring do not roll back by default; runtime exceptions do. Tune with rollbackFor. Timeouts abort long transactions. Read-only flags can hint the driver and the pool.
You reach for a transaction when several writes are one business event, or when a read must see a consistent snapshot. You keep autocommit for a single insert you can retry. You do not wrap an entire HTTP request that calls three other services; keep transactions short and on one database.
The cost of a long transaction is locks and a pool slot held. The cost of no transaction is a partial write. The cost of catching exceptions and still committing is a silent half-success.
Nested transactions are usually savepoints, not independent commits. Understand your framework. Distributed transactions (JTA, two-phase commit) are a last resort; prefer outbox patterns.
A good transaction is short and local. Read the row you will update, write, commit, then send the email. If you send the email inside the transaction and the broker is slow, you hold locks while the network waits. If you commit and then fail to send, you need a retry or an outbox table, not a longer transaction. Isolation cannot fix a distributed workflow. It can only make one database session consistent.
Code Examples
import java.sql.Connection;
import java.sql.PreparedStatement;
public class Transfer {
static void move(Connection c, long from, long to, int paise) throws Exception {
c.setAutoCommit(false);
try (PreparedStatement debit = c.prepareStatement(
"update accounts set bal = bal - ? where id = ?");
PreparedStatement credit = c.prepareStatement(
"update accounts set bal = bal + ? where id = ?")) {
debit.setInt(1, paise);
debit.setLong(2, from);
debit.executeUpdate();
credit.setInt(1, paise);
credit.setLong(2, to);
credit.executeUpdate();
c.commit();
} catch (Exception e) {
c.rollback();
throw e;
} finally {
c.setAutoCommit(true);
}
}
}Common Gotchas
Autocommit true means each statement is its own transaction
a failed second update keeps the first.
Checked exceptions do not roll back Spring transactions by default
Checked exceptions do not roll back Spring transactions by default
Long transactions hold connections and locks
Long transactions hold connections and locks