Created
February 23, 2011 21:14
-
-
Save l1x/841200 to your computer and use it in GitHub Desktop.
how to handle a transaction with mnesia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{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