Database / Mnesia intermediate to advanced Interview questions
What is a nested Mnesia transaction and how does it behave differently from a top-level one?
Calling mnesia:transaction/1 from inside code that's already running within another
transaction creates a nested transaction. Rather than being a fully independent transaction, it shares
the outer transaction's locks and only really commits when the outermost transaction itself commits — an
abort of the inner one aborts the whole outer transaction too.
mnesia:transaction(fun() -> mnesia:write(#a{id = 1}), mnesia:transaction(fun() -> %% nested mnesia:write(#b{id = 1}) end), mnesia:write(#c{id = 1}) end).
This differs from what "nested transaction" might suggest in some other databases (an independently committable sub-unit) — in Mnesia, there's really just one logical transaction underneath, and nesting mainly matters for code organization (letting a helper function wrap its own operations in a transaction without caring whether it's already inside one) rather than providing partial-commit semantics.
More Related questions...