Unix Technical Forum

reliably increasing a number in a concurrent setting

This is a discussion on reliably increasing a number in a concurrent setting within the MySQL forums, part of the Database Server Software category; --> I have a table with two fields, VARCHAR playername and INT score. When the player wins, I want to ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-03-2008, 02:49 PM
Dirk Groeneveld
 
Posts: n/a
Default reliably increasing a number in a concurrent setting

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.

This is a simplified example. In the real application I want to do
something more complex to the score, so a solution that only works for
doing simple arithmetic in a field won't work for me.

I can think of a few solutions to this problem, but they are all
cumbersome and prone to error. I can't help but feel that this must be a
very common problem that has a very common solution.

Thanks,
Dirk
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-03-2008, 02:49 PM
Willem Bogaerts
 
Posts: n/a
Default Re: reliably increasing a number in a concurrent setting

> 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.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-03-2008, 02:49 PM
Dirk Groeneveld
 
Posts: n/a
Default Re: reliably increasing a number in a concurrent setting

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?

Dirk
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-03-2008, 02:49 PM
Paul Lautman
 
Posts: n/a
Default Re: reliably increasing a number in a concurrent setting

Dirk Groeneveld 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?
>
> Dirk


Then you ask a different question stating what the new requirements are.
Different problems have different solutions.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #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


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-03-2008, 02:50 PM
Dirk Groeneveld
 
Posts: n/a
Default Re: reliably increasing a number in a concurrent setting

On Fri, 28 Mar 2008 16:38:34 +0000, Paul Lautman wrote:
> Dirk Groeneveld 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?

>
> Then you ask a different question stating what the new requirements are.
> Different problems have different solutions.


Remember the second paragraph of my original question? Here it is again,
for your benefit:

> This is a simplified example. In the real application I want to do
> something more complex to the score, so a solution that only works for
> doing simple arithmetic in a field won't work for me.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:53 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com