Sobes.tech
Middle

What is a transaction? Name the ACID principles.

sobes.tech AI

Answer from AI

A transaction is a sequence of operations on a database that are executed as a single unit. Either all operations complete successfully (commit), or in case of an error, all changes are rolled back (rollback), ensuring data integrity.

The ACID principles that guarantee transaction reliability:

  • Atomicity — the transaction is executed completely or not at all.
  • Consistency — after the transaction completes, the database remains in a correct state, satisfying all constraints.
  • Isolation — concurrent transactions do not affect each other as if they were executed sequentially.
  • Durability — after a transaction is committed, changes are saved even in case of system failures.

Example in SQL:

BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

Here, money transfer between accounts will either complete fully or not at all, preventing loss or duplication of funds.