Lab 14: Transactions

Time: 30 minutes | Level: Foundations | DB: MySQL 8 / PostgreSQL 15

Overview

Learn transaction fundamentals: BEGIN/COMMIT/ROLLBACK, SAVEPOINT, autocommit mode, and the classic fund transfer example demonstrating atomicity.


Step 1: ACID Properties

Property
Meaning
Example

Atomicity

All-or-nothing: the entire transaction succeeds or nothing changes

Transfer $100: debit AND credit must both succeed

Consistency

Database always moves from one valid state to another

Account balance can't go negative (CHECK constraint)

Isolation

Concurrent transactions don't interfere

Two transfers can't read stale balances

Durability

Committed data survives crashes

Power failure after COMMIT → data safe on disk


Step 2: Setup

docker run -d --name mysql-lab -e MYSQL_ROOT_PASSWORD=rootpass mysql:8.0
for i in $(seq 1 30); do docker exec mysql-lab mysql -uroot -prootpass -e "SELECT 1" 2>/dev/null && break || sleep 2; done

docker exec mysql-lab mysql -uroot -prootpass << 'EOF'
CREATE DATABASE bankdb;
USE bankdb;

CREATE TABLE accounts (
    account_id  INT          NOT NULL AUTO_INCREMENT,
    owner       VARCHAR(100) NOT NULL,
    balance     DECIMAL(12,2) NOT NULL DEFAULT 0.00,
    updated_at  TIMESTAMP    DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (account_id),
    CONSTRAINT chk_balance CHECK (balance >= 0)
);

CREATE TABLE transfers (
    transfer_id  INT NOT NULL AUTO_INCREMENT,
    from_account INT NOT NULL,
    to_account   INT NOT NULL,
    amount       DECIMAL(12,2) NOT NULL,
    transferred_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    status       VARCHAR(20) DEFAULT 'completed',
    PRIMARY KEY (transfer_id)
);

INSERT INTO accounts (owner, balance) VALUES
('Alice',  5000.00),
('Bob',    3000.00),
('Carol',  1000.00),
('David',    50.00);  -- low balance
EOF

Step 3: Autocommit Mode

📸 Verified Output:

💡 MySQL default: autocommit = ON. Each statement auto-commits. To use multi-statement transactions, use START TRANSACTION or BEGIN — this temporarily disables autocommit until COMMIT or ROLLBACK.


Step 4: Basic COMMIT

📸 Verified Output:


Step 5: ROLLBACK

📸 Verified Output:


Step 6: SAVEPOINT

📸 Verified Output:


Step 7: Atomic Fund Transfer (Full Example)

📸 Verified Output:


Step 8: Capstone — Verify Atomicity

📸 Verified Output:

Cleanup:


Summary

Command
Description

START TRANSACTION / BEGIN

Start a transaction (disables autocommit)

COMMIT

Permanently save all changes

ROLLBACK

Undo all changes since BEGIN

SAVEPOINT name

Mark a partial rollback point

ROLLBACK TO SAVEPOINT name

Undo to savepoint, transaction continues

RELEASE SAVEPOINT name

Remove a savepoint

SET autocommit = 0

Disable autocommit for session

SHOW VARIABLES LIKE 'autocommit'

Check current autocommit setting

Isolation Level
Dirty Read
Non-repeatable Read
Phantom Read

READ UNCOMMITTED

Possible

Possible

Possible

READ COMMITTED

No

Possible

Possible

REPEATABLE READ

No

No

Possible (MySQL: No)

SERIALIZABLE

No

No

No

MySQL default: REPEATABLE READ. PostgreSQL default: READ COMMITTED.

Next: Lab 15 — String Functions

Last updated