View Single Post

   
  #5 (permalink)  
Old 04-03-2008, 02:50 PM
toby
 
Posts: n/a
Default Re: reliably increasing a number in a concurrent setting

On Mar 28, 11:44 am, Dirk Groeneveld <groenev...@gmail.com> wrote:
> On Fri, 28 Mar 2008 08:35:07 +0100, Willem Bogaerts wrote:
> >> I have a table with two fields, VARCHAR playername and INT score. When
> >> the player wins, I want to increase her score. The player might play
> >> multiple games at the same time, so if I read the old score, increase
> >> it and write it back, I run the risk of overwriting a concurrent update
> >> that does the same thing.

>
> > Not if you do it in the same statement:

>
> > UPDATE yourtable SET score=score+1 WHERE playername='Some name';

>
> > Every single SQL statement is atomic, meaning that concurrency problems
> > do not occur within one statement.

>
> What if my operation is more complicated than increasing a number? What
> if instead of an INT score I have a BLOB image, and I want to do
> operations to the image like rotate it, blur it or something else? In
> short, what if my operation is something I can't do in SQL?


You may be able to take advantage of InnoDB lock modes, for example
SELECT ... FOR UPDATE. See:
http://dev.mysql.com/doc/refman/5.0/...ing-reads.html

>
> Dirk


Reply With Quote