PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.databases.mysql > Re: reliably increasing a number in a concurrent setting
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: reliably increasing a number in a concurrent setting

Réponse
 
LinkBack Outils de la discussion
Vieux 28/03/2008, 08h35   #1
Willem Bogaerts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/
  Réponse avec citation
Vieux 28/03/2008, 16h44   #2
Dirk Groeneveld
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 28/03/2008, 17h38   #3
Paul Lautman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Vieux 29/03/2008, 00h47   #4
toby
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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


  Réponse avec citation
Vieux 29/03/2008, 18h45   #5
Dirk Groeneveld
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h23.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12449 seconds with 13 queries