-
Notifications
You must be signed in to change notification settings - Fork 0
Concept Strict 2PL Locking
Turkce Dokumantasyon | English Documentation
Category: Core Concepts & Architecture
Subsystem: Concurrency & Transactions (AmberDB::Transact)
Entry Type: Architectural Concept
Strict Two-Phase Locking (Strict 2PL) is the concurrency control protocol implemented in AmberDB's transaction engine (AmberDB::Transact) and locking manager (flock_open). Under Strict 2PL:
-
Growing Phase: A transaction may acquire shared (
LOCK_SH) or exclusive (LOCK_EX) locks on multiple tables and record keys as needed, but may not release any locks during its execution. -
Shrinking Phase: All acquired locks are retained until the transaction explicitly commits (
transact_end/transact_commit) or rolls back (transact_rollback), at which point all locks are released atomically in a single burst.
This protocol mathematically guarantees Serializability (ACID Isolation) and completely prevents dirty reads, unrepeatable reads, lost updates, and cascading rollbacks across multi-process environments.
Strict 2PL Lifecycle in AmberDB
Growing Phase (Locks Acquired Dynamically)
- Table lock: catalog_product (LOCK_EX)
- Record lock: order_cart_1089 (LOCK_EX)
- Table lock: inventory_stock (LOCK_EX)
[Execution ]
Shrinking Phase (Atomic Release at End of Transaction)
Commit or Rollback ==> ALL locks released simultaneously
AmberDB supports two levels of OS-native flock lock granularity:
Locks the entire table file (dbstore/tables/${table}.lock). Used for batch ingestion (insert_list), table schema mutations, and index rebuilding (set_index).
- Shared read lock:
$adb->flock_open("catalog_product", "read"); - Exclusive write lock:
$adb->flock_open("catalog_product", "write");
Locks a specific record ID by creating or locking a record mutex file (dbstore/tables/${table}_${record_id}.lock).
- Exclusive record lock:
$adb->flock_open("orders", "write", 5001); - Release:
$adb->flock_close("orders", 5001);
# Stock deduction transaction under Strict 2PL
$adb->transact_start();
eval {
# 1. Read product and stock
my @product = $adb->read_id("catalog_product", 101);
my $current_stock = $product[4];
die "Out of stock\n" if $current_stock < 2;
# 2. Deduct stock and save
$product[4] = $current_stock - 2;
$adb->modify_id("catalog_product", @product);
# 3. Create order record
$adb->insert_id("orders", 0, 101, 2, "PAID", time());
# 4. Commit and atomically release all acquired locks
$adb->transact_end();
};
if ($@) {
# Error occurred: automatically executes LIFO rollback and releases locks
$adb->transact_rollback();
warn "Transaction failed: $@\n";
}AmberDB — High-Performance Schema-Driven NoSQL Database Engine for Perl.
Copyright 2005-2026 Maruf Cetin. Released under the Artistic License 2.0.
CPAN · GitHub Repository · Issue Tracker
- Berkeley DB (DB_File) Engine
- AmberDB Table Schema
- Global Flags
- Table Schema Flags
- Directory Structure
- File Structure (Extensions)
- Repeat Blocks
- Auto-Increment ID
- ASCII ID
- Relational Records
- Record Anatomy
- JOIN-Free Architecture
- Packed Binary Index
- Strict 2PL Locking
- Undo Journal & Rollback
- Tiered Junk Indexing
- Disjunctive Faceting
- Phonetic Accent Search
- 2-Pillar Disaster Recovery
- RAM-Disk Acceleration
- In-Memory Schema Mutation
- Simple Mode
- new
- config
- set_datadir
- insert_id
- insert_list
- modify_id
- modify_list
- delete_id
- delete_list
- read_id
- read_all
- read_list
- exist_id
- exist_list
- exist_table
- table_count
- table_keys
- table_lastid
- table_attr
- table_create
- field_fetch
- field_filter
- search_table
- facet_menu
- field_fltkeys
- field_allfltkeys
- facet_rules
- slug_read
- slug_fetch
- transact_start
- transact_end
- transact_commit
- transact_rollback
- transact_recover
- flock_open
- flock_close
- cache_setup
- cache_read
- cache_write
- cache_delete
- cache_preload
- cache_ensure
- buffer_write
- buffer_read
- buffer_delete
- recs_scan
- recs_get
- recs_put
- recs_del
- locale_uc
- locale_lc
- locale_sort
- locale_to_ascii
- locale_num2text
- locale_format_currency
- locale_format_date
- array_sort
- array_punch
- array_filter
- array_sublist
- deep_copy
- log_owner
- use_counter
- use_junk
- keep_deleted
- auto_id
- buffer_write
- simple
- no_write
- no_backup
- jnktype
- keys_only
- id_type
- language
- .db · .table · .dbase
- .inx · .fld · .src
- .fac · .srt · .slg
- .unq · .del · .aut
- .cnt · .txn · .amberdb
- .csv · .cache · .tmp