Skip to main content
Drizzle ORM provides a robust transaction API that ensures atomicity, consistency, isolation, and durability (ACID) for your database operations. Transactions work across PostgreSQL, MySQL, and SQLite with database-specific configuration options.

Import

Basic Transaction

db.transaction()

Execute multiple operations within a single transaction:
function
required
Async function that receives the transaction object and performs database operations
TransactionConfig
Database-specific transaction configuration
All operations within the callback are executed in a single transaction. If any operation fails or an error is thrown, the entire transaction is automatically rolled back.

Transaction Rollback

Automatic Rollback

Transactions automatically roll back when an error is thrown:

Manual Rollback

Explicitly roll back a transaction using tx.rollback():
Calling tx.rollback() throws a TransactionRollbackError internally, which causes the transaction to be rolled back gracefully.

PostgreSQL Transactions

Transaction Configuration

string
Transaction isolation level
  • 'read uncommitted'
  • 'read committed' (default)
  • 'repeatable read'
  • 'serializable'
string
Transaction access mode
  • 'read only'
  • 'read write' (default)
boolean
Whether the transaction can be deferred (only for read-only serializable transactions)

Setting Transaction Properties

You can modify transaction properties after it starts:

Isolation Levels Explained

Read Uncommitted: Lowest isolation level, allows dirty reads Read Committed: Prevents dirty reads, default for PostgreSQL Repeatable Read: Prevents dirty and non-repeatable reads Serializable: Highest isolation level, fully isolated from other transactions

MySQL Transactions

Transaction Configuration

string
required
Transaction isolation level
  • 'read uncommitted'
  • 'read committed'
  • 'repeatable read' (default)
  • 'serializable'
string
Transaction access mode
  • 'read only'
  • 'read write' (default)
boolean
Start transaction with a consistent snapshot (InnoDB)

SQLite Transactions

Transaction Configuration

string
Transaction behavior mode
  • 'deferred' (default) - Lock acquired on first read/write
  • 'immediate' - Write lock acquired immediately
  • 'exclusive' - Exclusive lock acquired immediately

Transaction Behaviors Explained

Deferred: No locks acquired until first database access. Good for read-mostly transactions. Immediate: Write lock acquired at transaction start. Prevents other write transactions from starting. Exclusive: Exclusive lock acquired immediately. No other transactions (read or write) can proceed.

Nested Transactions (Savepoints)

Drizzle supports nested transactions using savepoints:

PostgreSQL Savepoints

MySQL Savepoints

MySQL savepoints only work with the InnoDB storage engine.

SQLite Savepoints

Transaction Best Practices

Keep Transactions Short

Error Handling

Avoid External Side Effects

Read-Only Transactions

Optimize read-heavy operations:

Common Patterns

Batch Inserts with Dependency

Conditional Operations

Atomic Counter Updates