[erlang-questions] Mnesia transactions and error handling good practices
Andreas Hillqvist
andreas.hillqvist@REDACTED
Wed Jun 11 15:57:53 CEST 2008
4) Test and let it crash? (The erlang way)
{ok, archive} = archive_user(User_Id),
My guess is that the call to mnesia:transaction(F) returns something like
{aborted, badmatch}
By the way, what is the cost of nested transaction in mnesia?
Would it be "better practice" to keep this archive and set operation
within only on transaction?
Kind regards
Andreas Hillqvist
2008/6/11, Alexander Lamb <alexander.lamb@REDACTED>:
> Hello,
>
> I read in the Erlang manual about avoiding using too many exceptions
> as well as making the initial assumption that functions work.
>
> I have a practicle (simple) case:
>
> Each time I save a user (a record in a table), I want to archive the
> previous values of that record. For that purpose, I have a users table
> and a users_archive table.
>
> To archive the user, it is simply:
>
> archive_user(User_Id) ->
> F = fun() ->
> case mnesia:read({users,User_Id}) of
> [User] -> mnesia:write(users_archive,User#users{timestamp =
> calendar:universal_time()},write);
> _Any -> nothing
> end,
> case mnesia:transaction(F) of
> {atomic, _Result} -> {ok, archive};
> {aborted, Reason} -> {error, Reason}
> end.
>
> Now, to insert the new values, I do:
>
> set_user
> (User_Id
> ,System_Name
> ,Production_Type
> ,Email
> ,Last_Name,First_Name,Country,User_Status,Save_User_Id,User_Details) ->
> F = fun() ->
> archive_user(User_Id),
> mnesia:write(email,
> #users{user_id = User_Id, system_name = System_Name,
> production_type = Production_Type, email = Email, last_name =
> Last_Name, first_name = First_Name, country = Country, user_status =
> User_Status, timestamp = calendar:universal_time(), save_user_id =
> Save_User_Id, user_details = User_Details},
> write)
> end,
> case mnesia:transaction(F) of
> {atomic, _Any} -> {ok, erlang:binary_to_list(User_Id)};
> {aborted, Reason} -> {error, Reason}
> end.
>
> Now, obviously, this works as long as the "archive_user" function does
> not return {aborted, Reason}. Indeed, I don't test it.
>
> What is the good practice:
>
> 1) Not test "obvious" things (like in this example)
> 2) Test the response in cascade with case statements
> 3) Raise an exception in nested function in order to have the outer
> transaction fail
>
> Thanks,
>
> Alex
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
More information about the erlang-questions
mailing list