Java Transactions and isolation

Where correctness under concurrency is decided, and where the defaults differ enough between engines that the same code behaves differently.

4 concepts · 10 interview questions

What this topic covers

Every concept in transactions and isolation, and the questions each one gets asked as. Where a question links, it has a full write-up.

ACID

Atomicity, consistency, isolation and durability — four separate guarantees people usually recite as one.

  • What does ACID stand for, and which part does isolation level weaken?
  • How is atomicity actually implemented?

Isolation levels and anomalies

Four levels, each permitting a specific set of anomalies: dirty read, non-repeatable read, phantom read.

  • What are the isolation levels, and what does each permit?
  • What is a non-repeatable read versus a phantom read?
  • Which isolation level does your database default to?

Locking and deadlocks

Shared and exclusive locks, held to the end of a transaction, with deadlock when two transactions take them in opposite orders.

  • What causes a database deadlock, and how do you prevent it?
  • Optimistic versus pessimistic locking — when do you use each?
  • What does SELECT ... FOR UPDATE do?

Transactions across services

Two-phase commit is available and usually the wrong answer; sagas with compensating actions are what services actually do.

  • How do you maintain consistency across two services?
  • What is the outbox pattern, and what problem does it solve?

More in Databases and persistence

See the whole reference map →