[erlang-questions] Couple of questions about mnesia locking

ryeguy ryeguy1@REDACTED
Fri Apr 3 21:34:53 CEST 2009


If I wanted to check if a username is in use before registering an
account like this:

F=fun()->
case is_username_available(User) of    %% does a mnesia:read to see if
there is a record with that username
    false -> throw(username_in_use);
    true -> mnesia:write(User)
end,

mnesia:transaction(F).

Don't I have a potential race condition here? Since there is no record
to lock because it doesn't exist, how can mnesia guarantee another
process isn't also going to write that same Username to the database?
If I'm doing this wrong, what's the right way? A table lock (eww)?

My second question is regarding a quote from the mnesia manual:
"Write locks are normally acquired on all nodes where a replica of the
table resides (and is active). Read locks are acquired on one node
(the local one if a local replica exists). "

What does it mean read locks are acquired on one node? What would
happen when a table is distributed? Wouldn't that defeat the purpose?



More information about the erlang-questions mailing list