Database / Mnesia basics Interview questions
What is the difference between mnesia:read/1 and mnesia:wread/1?
Both fetch a record by key inside a transaction, but they take different lock types up front.
mnesia:read/1 takes a read lock, which is fine for a plain lookup, but if you intend to
immediately write back an updated version of that same record, a read lock can force an extra lock upgrade
step. mnesia:wread/1 takes a write lock right away instead.
mnesia:transaction(fun() -> [P] = mnesia:wread({person, 1}), mnesia:write(P#person{age = P#person.age + 1}) end).
Using wread/1 when you know you're about to write signals your intent up front, which can
reduce the chance of a deadlock between two transactions that both read-then-write the same record
concurrently — each avoids the read-lock-to-write-lock upgrade dance that could otherwise let two
transactions each hold a read lock and wait on each other for the write lock.
More Related questions...