473,386 Members | 1,766 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,386 software developers and data experts.

Isolation Level - Quick Question

To all SQL gurus:

I have a Windows Service that uses a single SQL Server table to
retrieve items of work. Each thread of the service checks this table
for the earliest item of work that is not already in process, marks
that item as in process, then begins to work the item. My concern is
whether the threads will begin to step on each other's toes by picking
the same item of work at the same time. To prevent this, I use the
following SQL table:

[WorkItems]
WorkItem varchar(512)
DateSubmitted datetime
Status int

In requesting the next work item, I use the following SQL syntax:

DECLARE @workitem varchar(512)
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
SELECT TOP 1 @workitem=WorkItemName FROM WorkItems WHERE Status=1
ORDER BY DateSubmitted
UPDATE WorkItems SET Status=2 WHERE WorkItemName=@workitem
SELECT * FROM WorkItems WHERE WorkItemName=@workitem
COMMIT TRANSACTION

The idea is that the Transaction Isolation Level, along with the three
statements in the transaction block, will only let one thread at a
time request the next work item. The three statements in the
transaction block select the next work item, mark it as in process,
then return the work item to the calling thread. In limited testing,
all seems well. Before going into production, however, I would like to
see if anyone can confirm that my ideas will indeed prevent threads
from duplicating each other's work.

Will the above SQL syntax allow me to run multiple threads all looking
to the same database table for work, but prevent them from selecting
any of the same work at the same time? If you need more information,
please ask.

Reply to newsgroup, or directly at ma************@NOSPAM.srcp.com.

Matthew Roberts
Jul 20 '05 #1
3 4348
Matthew Roberts (mr*********@hotmail.com) writes:
In requesting the next work item, I use the following SQL syntax:

DECLARE @workitem varchar(512)
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
BEGIN TRANSACTION
SELECT TOP 1 @workitem=WorkItemName FROM WorkItems WHERE Status=1
ORDER BY DateSubmitted
UPDATE WorkItems SET Status=2 WHERE WorkItemName=@workitem
SELECT * FROM WorkItems WHERE WorkItemName=@workitem
COMMIT TRANSACTION

The idea is that the Transaction Isolation Level, along with the three
statements in the transaction block, will only let one thread at a
time request the next work item. The three statements in the
transaction block select the next work item, mark it as in process,
then return the work item to the calling thread. In limited testing,
all seems well. Before going into production, however, I would like to
see if anyone can confirm that my ideas will indeed prevent threads
from duplicating each other's work.


I think you need to add locking hint to the first SELECT statement:

SELECT TOP 1 @workitem=WorkItemName
FROM WorkItems WITH (UPDLOCK) WHERE Status=1
ORDER BY DateSubmitted

Else two processes can read the same @workitem value, and proceed to
the update statement, and both will wait for each other, until the
deadlock detection mechanism rolls one of them back.

The UPDLOCK changes that, because only one process at a time can have
an UPDLOCK at a row. (But the lock does not block readers that only
requires shared locks.)

There is anothing here which makes me a little nervous. Is WorkItemName
unique in this table? Since it's a varchar(512) it does not look like
a typical primary key. If there could be more than one row with the
same work-item name theoretically, process A and process B could
read the same @workitemname from two different rows. Since the rows
would be different, they would not block each other.

Even if WorkItemName is unique, I would recommend you to put this in
the ORDER BY clause to make it completely deterministic in any given
situation which row you get.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
WorkItemName is indeed the primary key and corresponds to a filename,
hence the large size of the field. I realize that this is a bad
primary key, and it will be changed in production code, but for now it
works since I am just testing my methodology.

Now, as for the (UPDLOCK) directive, what is it's purpose and how will
it change things? I know the BOL answer, but I need something in
English, please. :)

FYI, when I use only the DateSubmitted field in the ORDER BY clause of
my query, everything seems to work fine. Using a series of text files
to determine which threads work on what, I can verify that only one
thread works on any given work item. So far anyway. If however, I add
the WorkItemName to the ORDER BY clause, exactly half of my threads
will fail out with deadlocking issues. So my question here is, is that
WorkItemName field really necessary in the ORDER BY clause? It
obviously makes a difference, but if I were to remove it, can I still
be guaranteed that each thread never grabs the same work item?
Jul 20 '05 #3
Matthew Roberts (mr*********@hotmail.com) writes:
Now, as for the (UPDLOCK) directive, what is it's purpose and how will
it change things? I know the BOL answer, but I need something in
English, please. :)
I think I tried to explain, but I'll recap.

A UPDLOCK is a shared lock, that is it does not prevent other processes
from reading that row (or page, table or whatever resource the lock is
on), but it blocks updates to that row. And, this is the gist: it also
blocks other UPDLOCK. That is, UPDLOCK informs SQL Server that you will
update the row.
FYI, when I use only the DateSubmitted field in the ORDER BY clause of
my query, everything seems to work fine.
As long as DateSubmitted is unique, there will not be surprises. Even if
it is not unique, you may not get surprises, but theoretically they
could occur.
So far anyway. If however, I add the WorkItemName to the ORDER BY
clause, exactly half of my threads will fail out with deadlocking
issues.


Did you use UPDLOCK? The purpose of UPDLOCK is to prevent deadlocking.

An interesting twist would be to add a READPAST hint to accompany the
UPDLOCK hint. This hint instructs SQL Server to skip blocked rows.
In this way, you would get better concurrency. However, this is not
something I have tested myself, so I cannot tell whether it works at
all.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

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

Similar topics

11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
2
by: klh | last post by:
We use DB2 Connect v 7.2 FP7 in Windows NT hitting a OS/390 DB2 v7.1 database. We have a Websphere (java) application that issues dynamic SQL. Most of the time when we issue dynamic SQL SELECT...
9
by: yu_sha | last post by:
Hello everyone We have a bunch of components registered under COM+ with 'transaction required' option. On the client we are using iSeries Access 5.2.0, with all possible fixes applied...
3
by: Eric Porter | last post by:
Dear All, I have a VB6.COM program using a VB6.COM DLL, which in turn uses ADODB that performs various bits of SQL. I have re-written the VB6.COM DLL in C#.NET (with a VB6 shell), which uses...
2
by: kanda | last post by:
Hello. I am developing the application (VBA&ODBC, to be exact) which periodically calls the stored procedures in the IBM DB2. A few of the procedures require executing with isolation level RR (...
5
by: m0002a | last post by:
Is there some way to track the isolation level of an indivual SQL statement submitted via JDBC in a snaphot or some other similar means? I have JDBC programs that are changing the isolation level...
3
by: D. | last post by:
I have a question about the "readCommitted" transaction isolation level. I have a client that is updating a record on a table. I suspend the execution after the UPDATE but before the commit...
3
by: RG | last post by:
How can I lookup the current isolation level? Thanks in advance
3
by: Maryan | last post by:
Hi everybody, there are two ways to change the isolation level: For instance i would like to change the isolation level to rs 1. "db2 change isolation to rs" 2. "db2 set current isolation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.