
Transaction Management
- 411 installs
- 305 repo stars
- Updated March 4, 2026
- aj-geddes/useful-ai-prompts
transaction-management is an agent skill that models ACID transactions, isolation levels, retries, and rollback paths for developers who need consistent multi-step database writes under concurrency.
About
transaction-management is an aj-geddes/useful-ai-prompts agent skill for implementing ACID-compliant database transactions on PostgreSQL and MySQL. It teaches BEGIN/COMMIT/ROLLBACK patterns for multi-statement transfers, isolation level selection, explicit row locking, deadlock detection, timeout configuration, and distributed transaction coordination for financial and inventory updates. Seven reference guides cover PostgreSQL and MySQL transactions, isolation levels, explicit locking, and deadlock prevention strategies. Developers reach for transaction-management when payment transfers must be atomic, inventory decrements race under concurrency, or partial failures require deterministic rollback instead of orphaned rows.
- Isolation level selection
- Rollback and compensation flows
- Idempotency and retry patterns
- Deadlock avoidance guidance
- ORM transaction scope examples
Transaction Management by the numbers
- 411 all-time installs (skills.sh)
- Ranked #143 of 911 Databases skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill transaction-managementAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 411 |
|---|---|
| repo stars | ★ 305 |
| Last updated | March 4, 2026 |
| Repository | aj-geddes/useful-ai-prompts ↗ |
How do you implement ACID database transactions?
Model ACID transactions, isolation levels, retries, and rollback paths so multi-step writes stay consistent under concurrency, partial failures, and payment or inventory updates.
Who is it for?
Backend developers writing PostgreSQL or MySQL code where payments, inventory, or ledger updates must stay consistent under concurrent writes.
Skip if: Developers on eventually consistent NoSQL stores who do not use SQL BEGIN/COMMIT transaction semantics.
When should I use this skill?
Implementing money transfers, inventory updates, choosing isolation levels, or debugging deadlocks and partial write failures.
What you get
Transaction-wrapped SQL, isolation level choices, locking strategies, rollback handlers, and deadlock prevention patterns.
- transaction-wrapped SQL
- isolation level policy
- deadlock retry strategy
By the numbers
- Bundles 7 reference guides for PostgreSQL and MySQL transactions and locking
- Quick-start demonstrates atomic two-account balance transfer with BEGIN/COMMIT
Files
Transaction Management
Table of Contents
Overview
Implement robust transaction management with ACID compliance, concurrency control, and error handling. Covers isolation levels, locking strategies, and deadlock resolution.
When to Use
- ACID transaction implementation
- Concurrent data modification handling
- Isolation level selection
- Deadlock prevention and resolution
- Transaction timeout configuration
- Distributed transaction coordination
- Financial transaction safety
Quick Start
Simple Transaction:
-- Start transaction
BEGIN;
-- Multiple statements
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- Commit changes
COMMIT;
-- Or rollback
ROLLBACK;Reference Guides
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| PostgreSQL Transactions | PostgreSQL Transactions |
| MySQL Transactions | MySQL Transactions |
| PostgreSQL Isolation Levels | PostgreSQL Isolation Levels |
| MySQL Isolation Levels | MySQL Isolation Levels |
| PostgreSQL Explicit Locking | PostgreSQL Explicit Locking |
| MySQL Locking | MySQL Locking |
| Deadlock Prevention | PostgreSQL - Deadlock Detection: |
Best Practices
✅ DO
- Follow established patterns and conventions
- Write clean, maintainable code
- Add appropriate documentation
- Test thoroughly before deploying
❌ DON'T
- Skip testing or validation
- Ignore error handling
- Hard-code configuration values
Deadlock Prevention
PostgreSQL - Deadlock Detection:
-- PostgreSQL automatically detects deadlocks
-- Kills one transaction and raises error
-- Example deadlock scenario
-- Transaction 1: Lock A, then try Lock B
-- Transaction 2: Lock B, then try Lock A
-- Result: One transaction rolled back with deadlock error
-- Retry logic
DO $$
DECLARE
retry_count INT := 0;
BEGIN
LOOP
BEGIN
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
EXIT;
EXCEPTION WHEN deadlocked_table THEN
ROLLBACK;
retry_count := retry_count + 1;
IF retry_count > 3 THEN
RAISE;
END IF;
-- Wait before retry
PERFORM pg_sleep(0.1);
END;
END LOOP;
END $$;MySQL - Deadlock Prevention:
-- Prevent deadlock by consistent lock ordering
-- Always lock in same order: table1 id=1, then table2 id=2
START TRANSACTION;
-- Always lock account 1 first, then account 2
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
SELECT * FROM accounts WHERE id = 2 FOR UPDATE;
-- Safe order prevents deadlock
COMMIT;Deadlock Recovery Handling:
// Application-level deadlock retry (Node.js)
async function transferMoney(fromId, toId, amount, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
await db.query("BEGIN");
await db.query(
"UPDATE accounts SET balance = balance - $1 WHERE id = $2 FOR UPDATE",
[amount, fromId],
);
await db.query(
"UPDATE accounts SET balance = balance + $1 WHERE id = $2 FOR UPDATE",
[amount, toId],
);
await db.query("COMMIT");
return { success: true };
} catch (error) {
if (error.code === "40P01") {
// Deadlock detected
await db.query("ROLLBACK");
if (i === retries - 1) throw error;
// Exponential backoff
await new Promise((r) => setTimeout(r, 100 * Math.pow(2, i)));
} else {
throw error;
}
}
}
}MySQL Isolation Levels
MySQL Isolation Levels
MySQL Isolation Level Configuration:
-- Check current isolation level
SHOW VARIABLES LIKE 'transaction_isolation';
-- Set for current session
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- Set for all new connections
SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- Set for specific transaction
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
-- Statements
COMMIT;Isolation Level Comparison:
-- READ UNCOMMITTED (dirty reads possible)
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
-- READ COMMITTED (repeatable reads, phantom reads possible)
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
-- REPEATABLE READ (phantom reads possible, MySQL default)
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- SERIALIZABLE (no anomalies)
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;MySQL Locking
MySQL Locking
Row-Level Locking:
-- Implicit locking on UPDATE/DELETE
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- Row is locked until transaction ends
COMMIT;
-- SELECT FOR UPDATE: explicit lock
START TRANSACTION;
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
-- Exclusive lock acquired
UPDATE accounts SET balance = 100 WHERE id = 1;
COMMIT;
-- SELECT FOR SHARE: read lock
START TRANSACTION;
SELECT * FROM accounts WHERE id = 1 FOR SHARE;
-- Shared lock (blocks FOR UPDATE)
COMMIT;Gap Locking (InnoDB):
-- InnoDB locks gaps between rows
START TRANSACTION;
-- Locks rows and gaps where id between 1 and 100
SELECT * FROM products WHERE id BETWEEN 1 AND 100 FOR UPDATE;
-- Prevents phantom rows in range
COMMIT;MySQL Transactions
MySQL Transactions
MySQL Transaction:
-- Start transaction
START TRANSACTION;
-- Or
BEGIN;
-- Statements
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- Commit
COMMIT;
-- Or rollback
ROLLBACK;MySQL Savepoints:
START TRANSACTION;
INSERT INTO orders (user_id, total) VALUES (123, 99.99);
SAVEPOINT after_insert;
UPDATE inventory SET quantity = quantity - 1 WHERE product_id = 456;
-- If inventory check fails
IF (SELECT quantity FROM inventory WHERE product_id = 456) < 0 THEN
ROLLBACK TO after_insert;
END IF;
COMMIT;PostgreSQL Explicit Locking
PostgreSQL Explicit Locking
Row-Level Locks:
-- FOR UPDATE: exclusive lock for update
BEGIN;
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
-- Other transactions cannot UPDATE/DELETE/SELECT FOR UPDATE this row
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;
-- FOR SHARE: shared lock
BEGIN;
SELECT * FROM accounts WHERE id = 1 FOR SHARE;
-- Other transactions can SELECT FOR SHARE but not FOR UPDATE
COMMIT;
-- FOR UPDATE NOWAIT: error if locked instead of waiting
BEGIN;
SELECT * FROM accounts WHERE id = 1 FOR UPDATE NOWAIT;
EXCEPTION WHEN OTHERS THEN
-- Row is locked
END;
COMMIT;Table-Level Locks:
-- Exclusive table lock
LOCK TABLE accounts IN EXCLUSIVE MODE;
-- No other transactions can access table
-- Share lock
LOCK TABLE accounts IN SHARE MODE;
-- Other transactions can read but not write
-- Exclusive for user access
LOCK TABLE accounts IN ACCESS EXCLUSIVE MODE;PostgreSQL Isolation Levels
PostgreSQL Isolation Levels
Read Uncommitted (not fully implemented):
-- PostgreSQL treats as READ COMMITTED
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN;
-- Can read uncommitted changes from other transactions
SELECT COUNT(*) FROM orders WHERE user_id = 123;
COMMIT;Read Committed (Default):
-- Default PostgreSQL isolation level
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
BEGIN;
-- Read committed data only
-- Allows phantom reads and non-repeatable reads
SELECT * FROM accounts WHERE id = 1;
-- May see different data if other transactions modify rows
SELECT * FROM accounts WHERE id = 1;
COMMIT;Repeatable Read:
-- Higher isolation level
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN;
-- Snapshot of data at transaction start
SELECT COUNT(*) as count_1 FROM orders;
-- Other transaction inserts order
-- Will still see same count
SELECT COUNT(*) as count_2 FROM orders;
COMMIT;Serializable:
-- Highest isolation level
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN;
-- Transactions execute as if serially
-- Prevents all anomalies (serialization failures may occur)
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT; -- May fail with serialization_failure errorPostgreSQL Transactions
PostgreSQL Transactions
Simple Transaction:
-- Start transaction
BEGIN;
-- Multiple statements
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- Commit changes
COMMIT;
-- Or rollback
ROLLBACK;Transaction with Error Handling:
BEGIN;
-- Savepoint for partial rollback
SAVEPOINT sp1;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
-- If error detected
IF (SELECT balance FROM accounts WHERE id = 1) < 0 THEN
ROLLBACK TO sp1;
-- Handle negative balance
END IF;
COMMIT;#!/bin/bash
# validate-schema.sh - Validate database schema
# Usage: ./validate-schema.sh <schema_file>
set -euo pipefail
SCHEMA_FILE="${{1:?Usage: $0 <schema_file>}}"
echo "Validating schema: $SCHEMA_FILE"
# TODO: Add schema validation
# - Check SQL syntax
# - Verify foreign key references
# - Check index definitions
# - Validate naming conventions
# - Check for missing constraints
echo "Schema validation complete."
-- Migration: [description]
-- Created: [date]
-- TODO: Customize for your migration framework
BEGIN;
-- Up migration
-- TODO: Add schema changes
-- CREATE TABLE IF NOT EXISTS ...
-- ALTER TABLE ...
-- Down migration (rollback)
-- TODO: Add rollback statements
-- DROP TABLE IF EXISTS ...
COMMIT;
Related skills
How it compares
Use transaction-management for SQL ACID semantics and isolation tuning; use an ORM-migration skill when schema changes—not transactional correctness—are the primary concern.
FAQ
Which databases does transaction-management cover?
transaction-management covers PostgreSQL and MySQL with seven reference guides on transactions, isolation levels, explicit locking, and deadlock prevention. The quick-start demonstrates a two-account balance transfer using BEGIN and COMMIT.
When should developers use transaction-management?
transaction-management applies when multi-step writes—payments, inventory, or ledger updates—must stay ACID-compliant under concurrency. The skill helps choose isolation levels, configure timeouts, and add rollback paths for partial failures.