[erlang-questions] increment an int in mysql vs. mnesia

Lev Walkin vlm@REDACTED
Mon Dec 3 04:52:31 CET 2007


Andrew Arrow wrote:
> in mysql I can say:
> 
>   update company set sales=sales+1 where company_id=123
> 
> and it will safely increment the value of sales by 1.
> 
> In mnesia I have to first read the value, then write the value+1.
> 
>   [Row] = mnesia:read({company, 123})
>   %% make Row1 = Row but with sales = sales + 1
>   mnesia:write(Row1)
>   mnesia:transaction(The_above_fun)
> 
> But it's possible for two processes at the same time to write back the
> same value for sales and miss one increment.
> i.e. processA reads 100 for sales, processB also reads 100, they each
> write back 101 but it should be 102.
> 
> Is there a better way to do this?  Thanks.

There's something on transactions in `erl -man mnesia`. I think it makes
sense to refer to documentation at this point.

-- 
Lev Walkin
vlm@REDACTED



More information about the erlang-questions mailing list