sqlintermediate
Optimistic Locking with Version Column
Prevent lost updates in concurrent environments using a version column for optimistic concurrency control.
sqlPress ⌘/Ctrl + Shift + C to copy
-- Add version column
ALTER TABLE products ADD COLUMN version INT NOT NULL DEFAULT 1;
-- Update with optimistic lock check
UPDATE products
SET
price = 29.99,
version = version + 1
WHERE
id = 42
AND version = 3; -- Expected version
-- Check if update succeeded (0 rows = conflict)
-- In application code:
-- if (result.rowCount === 0) throw new Error('Concurrent modification detected');Use Cases
- Multi-user editing
- Cart checkout
- Inventory management
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
sqladvanced
SQL Transaction Isolation Levels
Understand and use transaction isolation levels to control concurrency and data consistency.
Best for: Preventing race conditions in financial transactions
#sql#transactions
sqlintermediate
Deferred Foreign Key Constraints
Defer constraint checking to transaction commit for circular references and batch operations.
Best for: Circular references
#constraints#transactions
typescriptadvanced
Node.js In-Memory Task Queue
A simple in-memory task queue with concurrency control, retries, and priority support.
Best for: Rate-limited API call processing
#nodejs#queue
typescriptintermediate
Promise Concurrency Patterns
Master Promise.all, allSettled, race, any — parallel execution with error handling and timeouts.
Best for: Parallel API calls with error handling
#nodejs#promise