[erlang-questions] (not) understanding mnesia transactions

Seth Falcon seth@REDACTED
Thu Jun 18 07:34:59 CEST 2009


Chandru,

Thanks for the explanation.

* On 2009-06-17 at 23:57 +0100 Chandru wrote:
> Your code works as you expect it to if you first acquire a write
> lock on the table before you read from it.

Yes, that seems to fix it.

> So all your clients acquire read locks, decide that a record for a URL does
> not exist, and create a record. I'm not sure what locks are acquired for
> records which do not exist. I'm sure the answer lies in reading the mnesia
> source code...

Makes some sense.  I guess the big piece that I was missing is that
transactions can run concurrently and the programmer needs to manage
the locking.  A more subtle point, is that the locking mechanism is
such that once a lock is aquired it is kept for the remainder of the
transaction (I think).

So in the following example from the mnesia man page, the call to
mnesia:wread has the same effect as calling mnesia:lock and then
mnesia:read.

   raise(Name, Amount) ->
                  mnesia:transaction(fun() ->
                      case mnesia:wread({person, Name}) of
                          [P] ->
                              Salary = Amount + P#person.salary,
                              P2 = P#person{salary = Salary},
                              mnesia:write(P2);
                          _ ->
                              mnesia:abort("No such person")
                      end
                  end).



More information about the erlang-questions mailing list