Database / Mnesia basics Interview questions
What is a Mnesia table lock and how does it work?
Inside a transaction, Mnesia automatically acquires locks on the specific records (or, in some cases, the whole table) a transaction touches, so concurrent transactions can't corrupt each other's view of the data. Reads take a read lock; writes take a write lock, which is exclusive and blocks other transactions from reading or writing that same record until the holding transaction commits or aborts.
sequenceDiagram
participant T1
participant T2
participant Record
T1->>Record: write lock acquired
T2->>Record: attempts write, must wait
T1->>Record: transaction commits, lock released
Record-->>T2: lock granted, T2 proceeds
You don't request locks explicitly in ordinary code — mnesia:read/1 and
mnesia:write/1 acquire the appropriate lock automatically as part of running inside a transaction.
If two transactions would deadlock waiting on each other's locks, Mnesia detects it and aborts one of them,
which the calling code should be prepared to retry.
More Related questions...