Mnesia Transactions

When we added or removed data from the database or performed a query, we wrote the code something like this:

 
do_something(...) ->
 
F = ​fun​() ->
 
% ...
 
mnesia:write(Row)
 
% ... or ...
 
mnesia:delete(Oid)
 
% ... or ...
 
qlc:e(Q)
 
end​,
 
mnesia:transaction(F)

F is a fun with zero arguments. Inside F we called some combination of mnesia:write/1, mnesia:delete/1, or qlc:e(Q) (where Q is a query compiled with qlc:q/1). Having built the fun, we call mnesia:transaction(F), which evaluates the expression sequence in the fun.

Transactions guard against faulty program code but more importantly against concurrent access of the database. Suppose we have two processes that try to simultaneously access the same data. ...

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.