[erlang-questions] A style question

Ulf Wiger <>
Fri Feb 12 18:35:48 CET 2010


Fred Hebert wrote:
> Wouldn't using mnesia:activity/2 otherwise mean you've got to wrap every 
> transaction in a try ... catch yourself though? Is that considered an 
> annoyance or a wanted behaviour (outside of any gen_*)?

Whether it is an annoyance or not depends on what you consider
the sensible default. In my experience, most programs that run
mnesia transactions have no clever recourse if the transaction
aborts, so an exception would then be a good default.

It is not difficult to make that wrapper in one place:

transaction(F) ->
     transaction(transaction, F).

transaction(Type, F) ->
     try {atomic, mnesia:activity(Type, F)}
     catch
         exit:Reason ->
             {aborted, Reason}
     end.

Why would this be better than using mnesia:transaction/1,
you might ask.

There is an important difference between the two. mnesia:transaction/1
will always use mnesia.erl as a callback module, ignoring any setting
of the 'access_module' environment variable. mnesia:activity/2, on the
other hand, will check this and use any custom callback module you
may have selected as system default. One such access module is
mnesia_frag. Using mnesia:activity/2, you could potentially fragment
your table at a later time, having it be transparent to the program.


BR,
Ulf W

> On Fri, Feb 12, 2010 at 1:05 AM, Ulf Wiger 
> < <mailto:>> 
> wrote:
> 
>     There is an alternative that actually has a distinct advantage:
> 
>     mnesia:activity(
>        transaction,
>        fun my_transaction_fun/0)
> 
>     The advantage is that when tracing, you get much more readable
>     output. A disadvantage is that you forego the pleasure of inheriting
>     the environment.
> 
>     Note also the use of mnesia:activity/2 rather than transaction/1. It
>     has much better semantics, and is more flexible.
> 
>     BR,
>     Ulf W
> 
>     Ulf Wiger
>     CTO, Erlang Solutions, Ltd.
> 
>     ---------------------------------------------------
> 
>     WE'VE CHANGED NAMES!
> 
>     Since January 1st 2010 Erlang Training and Consulting Ltd. has
>     become ERLANG SOLUTIONS LTD.
> 
>     www.erlang-solutions.com <http://www.erlang-solutions.com>
> 
> 
>     ________________________________________________________________
>     erlang-questions (at) erlang.org <http://erlang.org> mailing list.
>     See http://www.erlang.org/faq.html
>     To unsubscribe; mailto:
>     <mailto:>
> 
> 


-- 
Ulf Wiger
CTO, Erlang Solutions Ltd, formerly Erlang Training & Consulting Ltd
http://www.erlang-solutions.com
---------------------------------------------------

---------------------------------------------------

WE'VE CHANGED NAMES!

Since January 1st 2010 Erlang Training and Consulting Ltd. has become ERLANG SOLUTIONS LTD.

www.erlang-solutions.com



More information about the erlang-questions mailing list