Skip to content

Instantly share code, notes, and snippets.

@l1x
Created February 23, 2011 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l1x/841200 to your computer and use it in GitHub Desktop.
Save l1x/841200 to your computer and use it in GitHub Desktop.
how to handle a transaction with mnesia
{atomic, Result} = mnesia:transaction(Fun)
case Result of [] -> undefined [Rec] -> Rec#rec.prop end.
%% http://www.erlang.org/doc/man/mnesia.html#transaction-2
add_family({family, F, M, Children}) ->
ChildOids = lists:map(fun oid/1, Children),
Trans = fun() ->
mnesia:write(F#person{children = ChildOids},
mnesia:write(M#person{children = ChildOids},
Write = fun(Child) -> mnesia:write(Child) end,
lists:foreach(Write, Children)
end,
mnesia:transaction(Trans).
oid(Rec) -> {element(1, Rec), element(2, Rec)}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment