Database / Mnesia basics Interview questions
Explain the lifecycle of a Mnesia transaction from start to commit?
Calling mnesia:transaction(Fun) kicks off a well-defined sequence: Mnesia runs your function,
tracking every read and write it performs, acquiring the appropriate locks as it goes, before deciding whether
the whole thing can be committed.
flowchart TD
A[mnesia:transaction Fun called] --> B[Fun executes: reads take read locks, writes take write locks]
B --> C{Fun completes without exception?}
C -->|Yes| D[Two-phase commit across all replicas of touched tables]
D --> E{All replicas prepared successfully?}
E -->|Yes| F["{atomic, Result} returned; locks released"]
E -->|No| G["{aborted, Reason} returned; no changes applied"]
C -->|No, exception raised| G
If the function runs to completion without an exception, Mnesia attempts to commit — for a replicated
table, this means the two-phase prepare/commit exchange across every node holding a copy. Only if every
involved replica confirms it can commit does the transaction actually take effect everywhere, returning
{atomic, Result}; any failure along the way aborts the whole thing, applying no changes and
returning {aborted, Reason} instead.
More Related questions...