HighTechTalks DotNet Forums  

What is the Safest Way to Add to Field?

Dotnet Framework (ADO.net) microsoft.public.dotnet.framework.adonet


Discuss What is the Safest Way to Add to Field? in the Dotnet Framework (ADO.net) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Jonathan Wood
 
Posts: n/a

Default What is the Safest Way to Add to Field? - 01-03-2008 , 04:14 PM






I have a numeric field. Users will modify it by adding a value to it.

But I'm a little concerned about the unlikely event where two users are
incrementing it at the same time. Unless there is some sort of lock between
the time the old value is read and the sum of the two values is written, one
change could override another.

Any suggestions?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com


Reply With Quote
  #2  
Old   
Jim Rand
 
Posts: n/a

Default Re: What is the Safest Way to Add to Field? - 01-03-2008 , 04:47 PM






Assuming you are using some sort of database, mark the transaction as
serializable and include a WHERE clause with the old value.

"Jonathan Wood" <jwood (AT) softcircuits (DOT) com> wrote

Quote:
I have a numeric field. Users will modify it by adding a value to it.

But I'm a little concerned about the unlikely event where two users are
incrementing it at the same time. Unless there is some sort of lock
between the time the old value is read and the sum of the two values is
written, one change could override another.

Any suggestions?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com




Reply With Quote
  #3  
Old   
Kerry Moorman
 
Posts: n/a

Default RE: What is the Safest Way to Add to Field? - 01-03-2008 , 05:43 PM



Jonathan,

I don't understand how this particular example would need to be handled any
differently than your normal concurrency handling.

Kerry Moorman


"Jonathan Wood" wrote:

Quote:
I have a numeric field. Users will modify it by adding a value to it.

But I'm a little concerned about the unlikely event where two users are
incrementing it at the same time. Unless there is some sort of lock between
the time the old value is read and the sum of the two values is written, one
change could override another.

Any suggestions?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com



Reply With Quote
  #4  
Old   
Jonathan Wood
 
Posts: n/a

Default Re: What is the Safest Way to Add to Field? - 01-03-2008 , 06:04 PM



If I want to set a field to a specific value, I can just set it. If it
overrides another update, that's fine because someone set the value after
someone else.

On the other hand, if I want to increment a value, say, 10, it's possible
that two users read the old value, the first one writes an 11 and then the
second one writes an 11. In this case, the second update was not an
increment, as expected.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Kerry Moorman" <KerryMoorman (AT) discussions (DOT) microsoft.com> wrote

Quote:
Jonathan,

I don't understand how this particular example would need to be handled
any
differently than your normal concurrency handling.

Kerry Moorman


"Jonathan Wood" wrote:

I have a numeric field. Users will modify it by adding a value to it.

But I'm a little concerned about the unlikely event where two users are
incrementing it at the same time. Unless there is some sort of lock
between
the time the old value is read and the sum of the two values is written,
one
change could override another.

Any suggestions?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com




Reply With Quote
  #5  
Old   
Jonathan Wood
 
Posts: n/a

Default Re: What is the Safest Way to Add to Field? - 01-03-2008 , 06:09 PM



Yes, of course it would just fail then if the WHERE didn't match.

Could I trouble you to explain the advantage of marking the transaction as
serializable?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Jim Rand" <jimrand (AT) ix (DOT) netcom.com> wrote

Quote:
Assuming you are using some sort of database, mark the transaction as
serializable and include a WHERE clause with the old value.

"Jonathan Wood" <jwood (AT) softcircuits (DOT) com> wrote in message
news:%23Amxi2kTIHA.1168 (AT) TK2MSFTNGP02 (DOT) phx.gbl...
I have a numeric field. Users will modify it by adding a value to it.

But I'm a little concerned about the unlikely event where two users are
incrementing it at the same time. Unless there is some sort of lock
between the time the old value is read and the sum of the two values is
written, one change could override another.

Any suggestions?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com





Reply With Quote
  #6  
Old   
Kerry Moorman
 
Posts: n/a

Default Re: What is the Safest Way to Add to Field? - 01-03-2008 , 07:20 PM



Jonathan,

So you are saying that you generally handle concurrency with the "last in
wins" strategy?

Kerry Moorman


"Jonathan Wood" wrote:

Quote:
If I want to set a field to a specific value, I can just set it. If it
overrides another update, that's fine because someone set the value after
someone else.

On the other hand, if I want to increment a value, say, 10, it's possible
that two users read the old value, the first one writes an 11 and then the
second one writes an 11. In this case, the second update was not an
increment, as expected.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Kerry Moorman" <KerryMoorman (AT) discussions (DOT) microsoft.com> wrote in message
news:EF5713A9-77FC-48F3-8A18-BDDDCEF940AF (AT) microsoft (DOT) com...
Jonathan,

I don't understand how this particular example would need to be handled
any
differently than your normal concurrency handling.

Kerry Moorman


"Jonathan Wood" wrote:

I have a numeric field. Users will modify it by adding a value to it.

But I'm a little concerned about the unlikely event where two users are
incrementing it at the same time. Unless there is some sort of lock
between
the time the old value is read and the sum of the two values is written,
one
change could override another.

Any suggestions?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com





Reply With Quote
  #7  
Old   
Jonathan Wood
 
Posts: n/a

Default Re: What is the Safest Way to Add to Field? - 01-03-2008 , 11:51 PM



Kerry,

Quote:
So you are saying that you generally handle concurrency with the "last in
wins" strategy?
Why not? If someone wants to set a field to one value, and someone later
wants to set it to another (even if only milliseconds later), why should I
prevent the field from being changed just because it had been changed
earlier?

Jonathan



Reply With Quote
  #8  
Old   
Cor Ligthert[MVP]
 
Posts: n/a

Default Re: What is the Safest Way to Add to Field? - 01-04-2008 , 01:01 AM



Jonathan,

The safest, create an extra (child) table where in you keep the mutations.
The sum of those is than the value of the field.
AFAIK you have In fact than never a concurrency problem because it are all
new inserted rows.

Cor


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.