[erlang-questions] (not) understanding mnesia transactions

Seth Falcon <>
Fri Jun 19 17:48:51 CEST 2009


Hi again,

I'm hoping to get a bit more clarification on what to expect when
transactions fail due to lock contention.

* On 2009-06-18 at 12:05 +0200 Dan Gudmundsson wrote:
> Except for io:format("Expecting ~p~n", [lists:seq(1, 10)]), which is wrong,
> since you use dirty_update_counter which will be invoked
> several times when there is a lock conflict on the write lock.

I think I understand the issue with transaction code possibly getting
executed multiple times, but for a transaction like:

    mnesia:transaction(fun() ->
        mnesia:lock({table, foo}, write),
        Id = mnesia:dirty_update_counter(counter, id, 1),
        New = #foo{ code = Id, created = now() },
        mnesia:write(New)
    end)

The docs seem to suggest that mnesia:lock will exit if it can't obtain
a lock.  In that case, one would assume that we don't end up
incrementing the counter.  Once the lock _is_ aquired, I would imagine
it would be a very exceptional case in which the transaction bombs
out.  So now I'm thinking that the expectation that the result gives
consequitve elements from the counter is reasonable (and at least a
small amount of testing seems to bear this out).  Or am I missing
another detail?

Cheers,

+ seth


More information about the erlang-questions mailing list