Database / Mnesia basics Interview questions
What is a Mnesia transaction?
A Mnesia transaction wraps a set of reads and writes so they either all succeed together or none of them take effect — the same all-or-nothing guarantee a traditional database transaction provides, implemented here across Erlang processes and, if the table is replicated, across nodes.
mnesia:transaction(fun() -> mnesia:write(#person{id = 1, name = "Ada", age = 34}), mnesia:write(#person{id = 2, name = "Grace", age = 41}) end).
The function passed to mnesia:transaction/1 runs with automatic locking and rollback: if any
operation inside it fails (a conflict, an exception), Mnesia aborts the whole transaction and none of the
writes are applied. This is the safe, default way to interact with Mnesia data, and is what you should reach
for unless you have a specific reason to use faster, less safe "dirty" operations instead.
More Related questions...