Transactions and ACID
Transactions group multiple database operations into one reliable unit. This matters for payments, order creation, inventory, and any workflow where partial data is dangerous.
Transaction Flow
BEGIN;
UPDATE accounts
SET balance = balance - 500
WHERE id = 1;
UPDATE accounts
SET balance = balance + 500
WHERE id = 2;
COMMIT;
Rollback
Use ROLLBACK when any step fails. In testing, verify that failed workflows do not leave half-created records.
BEGIN;
INSERT INTO orders(customer_id, status) VALUES (101, 'CREATED');
-- payment failed
ROLLBACK;
ACID
| Property | Meaning |
|---|---|
| Atomicity | All steps succeed or none do. |
| Consistency | Rules and constraints remain valid. |
| Isolation | Parallel transactions do not corrupt each other. |
| Durability | Committed data survives failures. |
QA Scenarios
- Cancel a payment mid-flow and verify no paid order exists.
- Retry the same API request and check duplicate prevention.
- Run two updates at the same time and verify final stock is correct.