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

Andrew Arrow oneone@REDACTED
Mon Dec 3 03:27:30 CET 2007


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.



More information about the erlang-questions mailing list