Sub topic 1
What a Transaction Is and Why It Exists
A transaction is a group of database operations that the database treats as one unit of work. The operations may include INSERT, UPDATE, and DELETE statements. A transaction is useful when the operations must succeed together.
For example, transferring money requires two changes:
- Subtract money from the sender's account.
- Add the same money to the receiver's account.
If only the first change succeeds, money disappears. If only the second succeeds, the database creates money. A transaction prevents either partial result from becoming permanent.
Prerequisites
You should know basic SQL statements such as SELECT, INSERT, and UPDATE. You should also understand tables, primary keys, and constraints. This chapter assumes that multiple application requests can use the same database at the same time.
The Transaction Boundary
A transaction has a boundary. The boundary begins when the database starts grouping statements and ends when the application either commits or rolls back the work.
BEGINstarts a transaction.COMMITmakes its changes permanent.ROLLBACKdiscards its changes.
The following example uses PostgreSQL syntax and can be run as a complete sequence in a database session:
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric(12, 2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts (id, balance)
VALUES (1, 100.00), (2, 50.00);
BEGIN;
UPDATE accounts
SET balance = balance - 30
WHERE id = 1;
UPDATE accounts
SET balance = balance + 30
WHERE id = 2;
COMMIT;
SELECT id, balance
FROM accounts
ORDER BY id;After the transaction commits, account 1 has 70.00 and account 2 has 80.00. Other database sessions see the completed transfer rather than a permanent half-transfer.
When to Use a Transaction
Group statements into one transaction when they represent one business action or when one statement depends on another. Common examples include:
- Creating an order and its order items.
- Reserving inventory and recording the reservation.
- Moving money between accounts.
- Updating a record and writing its audit entry.
- Deleting a parent record and its dependent records when the schema allows it.
Do not automatically place an entire long-running workflow inside one transaction. A transaction that stays open for a long time can hold locks, use resources, and increase conflicts with other sessions. Keep the boundary around the smallest complete unit of work.
| Operation pattern | Usually one transaction? | Reason |
|---|---|---|
| Debit one account and credit another | Yes | Both changes form one transfer |
| Generate a report from many tables | Usually no write transaction | A read-only snapshot may be enough |
| Send an email and update a database row | Carefully designed | Email delivery cannot normally be rolled back by the database |
The four ACID properties describe the guarantees transactions aim to provide: atomicity, consistency, isolation, and durability. The next chapters examine each property and the mechanisms databases use to provide it.
Exercise
Why should a debit and a credit normally be placed in the same transaction?
Show answer
They represent one business action. If either operation fails, rolling back both prevents the database from permanently losing or creating money.