[erlang-questions] Interesting behavior of mnesia:transaction/1

Joel Reymont joelr1@REDACTED
Wed Jul 4 14:04:39 CEST 2007


Folks,

I'm running two processes that both write to the same Mnesia table to  
update the same object. Each process runs this code:

%%% Fetch record, bump counter, store

bump(Parent, _, _, 0) ->
     Parent ! {done, self()},
     ok;

bump(Parent, Cache, K, N) ->
     F = fun() ->
                 {ok, _, L} = cache:fetch(Cache, K),
                 V = binary_to_term(list_to_binary(L)),
                 V1 = V + 1,
                 L1 = binary_to_list(term_to_binary(V1)),
                 X = cache:store(Cache, K, L1, "", "text/plain"),
                 {V1, X}
         end,
     cache:transaction(F),
     erlang:yield(),
     bump(Parent, Cache, K, N - 1).

where cache:transaction/1 is just a wrapper around mnesia:transaction/1.

I put a delay of 200ms into cache:store/5 and start process #2 with a  
100ms delay.

I was expecting to see processes take turns but instead I'm seeing  
each process run twice. Is there any explanation for this?

bump <0.904.0>: V = ok, Result = 1
bump <0.904.0>: V = ok, Result = 2
bump <0.905.0>: V = ok, Result = 3
bump <0.905.0>: V = ok, Result = 4
bump <0.904.0>: V = ok, Result = 5
bump <0.904.0>: V = ok, Result = 6
bump <0.905.0>: V = ok, Result = 7
bump <0.905.0>: V = ok, Result = 8
bump <0.904.0>: V = ok, Result = 9
bump <0.904.0>: V = ok, Result = 10
bump <0.905.0>: V = ok, Result = 11
bump <0.905.0>: V = ok, Result = 12

	Thanks, Joel

--
http://topdog.cc      - EasyLanguage to C# compiler
http://wagerlabs.com  - Blog








More information about the erlang-questions mailing list