473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Concurrency, have I got this more or less right?

Following on from a thread I started about "concurrency" (real-time-ish
system), I thought I would play about to see if I could easily adapt my data
model to take account of potential multi-user write conflicts. So, I would
appreciate you checking my logic/reasoning to see if this kind of thing
will work. Below I have a stored procedure that will simply delete a given
record from a given table. I have appended a "_Written" counter to the
columns of the table. Every time the record is written, the counter is
incremented. Clients store the current _Written count in their objects and
pass this in to any write procedure executed.

The procedure explicitly checks the _Written count within the transaction to
see if it agress with the written count passed in by the client. If it does
not, the client throws an error. Note I am explicitly checking the
_Written count precisely so I can determine exactly why this operation might
fail, rather than checking @@ROWCOUNT after an update.

Thanks.
Robin

CREATE PROCEDURE dbo.proc_DS_Remove_DataSet

@_In_ID INTEGER,
@_In_Written INTEGER

AS

DECLARE @Error INTEGER
DECLARE @WRITTEN INTEGER

BEGIN TRANSACTION
SET @Error = @@ERROR

IF @Error = 0
BEGIN

SELECT @WRITTEN = _Written FROM MyTable WHERE ID = @_In_ID
SET @Error = @@ERROR

IF @WRITTEN <> @_In_Written
BEGIN
RAISERROR ('10', 16, 1)
SET @Error = @@ERROR
END

END

IF @Error = 0
BEGIN
DELETE FROM MyTable WHERE ID = @_In_ID
SET @Error = @@ERROR
END

IF @Error = 0
COMMIT TRANSACTION
ELSE
ROLLBACK TRANSACTION

RETURN @Error
Jul 23 '05 #1
5 1493
You can use binary_checksum to check for changes without having to
append an extra field onto every table. If any field in the row
changes, the checksum will change.

Jul 23 '05 #2
On Fri, 21 Jan 2005 13:36:12 -0000, Robin Tucker wrote:
I would
appreciate you checking my logic/reasoning to see if this kind of thing
will work.

(snip)

Hi Robin,

It will work, but some remarks nonetheless.

1. Consider using a timestamp column instead of your _Written column. SQL
Server will automaticall update the value of the timestamp column whenever
the row is inserted or updated; you never have to include it in your code.
But you can use it to check that the row is untouched the same way you
propose to do it with your self-made _Written column.
(Note: despite the name, the timestamp value is in no way related to date
or time of latest update. The synonym for timestamp is rowversion, which
describes the actual function of this datatype lots better)

2. Even if you check that _Written (or the timestamp column) is unchanged
before doing the delete, I'd recommend you to still include
WHERE _Written = @WRITTEN
in the actual delete statement. There's always the theoretical possibility
that someone updates the row in the split microsecond between the SELECT
and the DELETE.
An alternative to this is to set the transaction isolation level to
REPEATABLE READ or to use the HOLDLOCK locking hint on the select. This
ensures that the lock used to read the row is not released after the read
operation, but retained until the transaction is finished, precluding any
changes from other connections to the row.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #3
Robin Tucker (id*************************@reallyidont.com) writes:
CREATE PROCEDURE dbo.proc_DS_Remove_DataSet

@_In_ID INTEGER,
@_In_Written INTEGER

AS

DECLARE @Error INTEGER
DECLARE @WRITTEN INTEGER

BEGIN TRANSACTION
SET @Error = @@ERROR

IF @Error = 0
BEGIN

SELECT @WRITTEN = _Written FROM MyTable WHERE ID = @_In_ID
SET @Error = @@ERROR


You should have "WITH (UPDLOCK)" after MyTable where. This is a shared
lock, so the row is still readable for others. But no other can get a
second UPDLOCK. This prevents other process from coming inbetween and
modifying the row.

Hugo suggested HOLDLOCK, but HOLDLOCK is a read-only lock. If two
processes attacks the same row simultaneously, you will get a deadlock,
because both get their HOLDLOCK, and none can delete because of the
other.

Using timestamps that Hugo suggested is an excellent idea.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
On Fri, 21 Jan 2005 22:53:20 +0000 (UTC), Erland Sommarskog wrote:
You should have "WITH (UPDLOCK)" (snip)Hugo suggested HOLDLOCK, but HOLDLOCK is a read-only lock.


Oops! You're right - I just keep interchanging these two.

Thanks for the correction, Erland!

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #5
Thanks both of you for your replies. Somewhere I missed the "timestamp"
type and yes that would be more suitable. Also will read up on locking
semantics as I was under the impression everything within my transaction
would be applied with the given rows locked for updates any way.

Thanks.

"Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message
news:p6********************************@4ax.com...
On Fri, 21 Jan 2005 22:53:20 +0000 (UTC), Erland Sommarskog wrote:
You should have "WITH (UPDLOCK)"

(snip)
Hugo suggested HOLDLOCK, but HOLDLOCK is a read-only lock.


Oops! You're right - I just keep interchanging these two.

Thanks for the correction, Erland!

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: aurora | last post by:
Hello! Just gone though an article via Slashdot titled "The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software" http://www.gotw.ca/publications/concurrency-ddj.htm]. It argues...
2
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data...
3
by: Robert Schuldenfrei | last post by:
Hi NG, I am looking for an opinion here. I am new to C# and SQL, being an old COBOL hand. I have started into a conversion of an old COBOL ERP system. I have a number of functions working now...
5
by: John Rivers | last post by:
Hello, The common approaches to concurrency control in web apps: optimistic: - row version (timestamp, guid, datetime, digest) etc. - value checking pessimistic: - locking fields /...
5
by: Vayse | last post by:
In my save code, most of items save fine. But sometimes I get a concurrency violation message. "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records." It happens on the...
9
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more...
4
by: Bob | last post by:
While testing my my program I came up with a consistency exception. My program consists of three datagridviews, One called dgvPostes which is the parent grid and its two children,one called...
8
by: Roger.Noreply | last post by:
Hi, Sql-Server 2000, 2005. A report fetches a lot of rows using the "WITH (ROWLOCK)" syntax (the sql is generated on the fly by a tool and not easily changeable). SELECT col1, col2 FROM mytab...
7
by: John | last post by:
Hi I have asked this question before but have not received any clear answer. I have a winform app with bound controls via dataadapter/dataset combination.I have a dbconcurrency exception ex. Now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.