Transactions: Two viewpoints
Simple transaction control in SQL
Example
Window opens
START TRANSACTION
INSERT … (empty item, only key)
OK button
UPDATE … (fields)
COMMIT
Saves this item’s fields as well as all the 1-many related items that were added
Cancel button
ROLLBACK
Deletes this item as well as all the 1-many related items that were added
Window opens
SAVEPOINT add-image
INSERT … (empty item, only key, fk)
OK button
UPDATE … (image caption field)
Cancel button
ROLLBACK to add-image
ACID properties of a transaction
RDBMS Subsystem support
Concurrency system support for isolation
Transaction isolation levels
Concurrency
Transactions in psycopg
Example transaction
Example transaction (MySQL)
set autocommit=0;
START transaction;
INSERT INTO workshop (state, title, description,
capacity, max_repeat, event_year)
VALUES ('Proposed', 'Cool Science Stuff',
'Amazing activities', 12, 3, 2024);
INSERT INTO person_workshop (person_email, workshop_id, role)
VALUES ('petersva@jmu.edu', LAST_INSERT_ID(), 'Lead');
COMMIT;
-- or ROLLBACK
autocommit is on by default in Workbench
Get the most recent autoincrement value
Example transaction (Postgres)
BEGIN;
INSERT INTO workshop (state, title, description,
capacity, max_repeat, event_year)
VALUES ('Proposed', 'Cool Science Stuff', 'Amazing activities',
12, 3, 2024);
INSERT INTO person_workshop (person_email, workshop_id, role)
VALUES ('petershz@jmu.edu', lastval(), 'Lead');
COMMIT;
-- or ROLLBACK
autocommit is off by default in PgAdmin, BEGIN is more common than START
Get the most recent autoincrement value