QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Changing data and transactions

What is a transaction and how does QA use it?

A transaction runs several operations as one: either all changes are applied or none. BEGIN opens it, COMMIT persists the changes, ROLLBACK cancels them, and without BEGIN every statement is a transaction of its own (autocommit). QA uses a transaction as insurance for its own changes and as a subject for testing failures between steps. In PostgreSQL an error makes the transaction aborted until ROLLBACK, and id sequences are not rolled back.

SQL · Changing data and transactions

A transfer of 100: the debit went through and the credit failed. Where is the money now?

A transfer consists of two updates: minus on one account, plus on another. If the credit fails and the debit stays, the money has vanished. A transaction guarantees that either both changes happen or neither. QA uses it in two ways: as insurance for its own changes and as a subject of testing.

How it works and why. BEGIN opens a transaction, COMMIT persists all changes, ROLLBACK cancels all of them. Without BEGIN every statement is a transaction of its own (autocommit). Until the transaction ends, only it sees its changes: other sessions at Read Committed, the PostgreSQL default, read the previous values. The changed rows stay locked against other writes until the end. PostgreSQL has two more traits. After an error inside a transaction it becomes aborted: every next command is rejected until ROLLBACK. And sequences (ids) are not rolled back: an insert followed by ROLLBACK leaves a gap in the numbering.

Example: a transfer that must not go through. Account 10 holds 150, account 11 holds 20, and the table has CHECK (balance >= 0). A transfer of 200:

BEGIN;
UPDATE accounts SET balance = balance - 200 WHERE id = 10;
-- ERROR:  new row for relation "accounts" violates check
--         constraint "accounts_balance_check"   (shortened)
UPDATE accounts SET balance = balance + 200 WHERE id = 11;
-- ERROR:  current transaction is aborted, commands ignored
--         until end of transaction block
ROLLBACK;

SELECT id, balance FROM accounts WHERE id IN (10, 11);
idbalance
10150.00
1120.00

The balances did not change: the first update was rejected, the second one to account 11 never ran, and ROLLBACK closed the aborted transaction. For a transfer of 100 both UPDATEs succeed, and after COMMIT the table shows 50.00 and 120.00.

Two half-operations: without a transaction and inside one

No transaction

Each UPDATE commits itself

The debit is committed, the credit failed: 100 vanished and there is nothing to roll back.

In a transaction

BEGIN … COMMIT

An error at any step, and ROLLBACK returns both accounts to the start.

Figure 1. A transaction turns several steps into one action.

How QA uses transactions:

  • Safe manual change: BEGIN, change, check, COMMIT or ROLLBACK.
  • Dry run: run the change, look at the effects, roll back.
  • Failure testing: stop the application or database between two steps of an operation and check that the state is not "half-done".

Limits and alternatives. A test-level rollback (a transaction rolled back after each test) works only when the test and the code under test share one connection. In E2E the application has its own connections and will not see uncommitted data, so the data is cleaned explicitly there. In MySQL DDL statements (CREATE, ALTER, DROP, TRUNCATE) end the transaction implicitly, in PostgreSQL they do not. Partial rollbacks are given by SAVEPOINT and ROLLBACK TO.

Common mistakes.

  • A forgotten COMMIT or ROLLBACK: the session hangs "idle in transaction", holds locks and others' updates wait.
  • Continuing to type commands after an error without ROLLBACK: in PostgreSQL they are all rejected.
  • Being surprised by an id gap after a rollback: a sequence does not go back, and that is normal.
  • A long transaction on a shared environment: it blocks colleagues and prevents cleaning of old row versions.
  • Expecting another session to see the changes before COMMIT.

Check yourself

In a PostgreSQL transaction the first UPDATE ended with an error. Will a second UPDATE run if you send it?

Answer

No. The transaction is aborted: commands are rejected with current transaction is aborted until ROLLBACK (or a rollback to a SAVEPOINT).

The last insert had id 100, then BEGIN; INSERT …; ROLLBACK;, then one more insert. What is its id?

Answer

102. The rolled-back insert used 101 and the sequence does not go back.

In an interview. Junior: A transaction runs several operations as one: all or none. A transfer debits one account and credits another, and both steps must go together. I use BEGIN, COMMIT and ROLLBACK to change data safely. Middle/Senior: Without BEGIN autocommit applies. In PostgreSQL an error aborts the transaction until ROLLBACK, and sequences are not rolled back. I test that a failure between steps leaves no half-done state, and I remember that a test-level rollback does not work when the application has its own connections.

More articles in this chapter