Solved

Need to prevent race condition during select and update

Posted on 2001-08-07
6
610 Views
Last Modified: 2012-06-27
I am using SQL Server 6.5 and I have a stored procedure that creates a sales number by incrementing the last one used.
I store that information in a table called  LastSalesNumber.  This function worked fine for over a
year but I am now having problems with an increase in traffic and users.  I have had at least two occasions where it appears that two users made sales at the very same time and two different sales records were created with the same number.  What changes should I make to the following code in the form of locking or restructuring to ensure that this doesn't happen anymore?


-- begin snippet
/* Assign Sales number to be last sales number assigned + 1  */

select @SalesNumber = "SLS-" + convert(varchar(13), LastNumber + 1)
     from LastSalesNumber


/*  increment the last Sales number assigned  */

update LastSalesNumber
     set LastNumber = LastNumber + 1

-- end snippet
0
Comment
Question by:geowilli
6 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Since there is no implict row locking in SQL Server 6.5 you may want to try reversing the statements:

In other words, update first
update LastSalesNumber
    set LastNumber = LastNumber + 1

And then Select:
select @SalesNumber = "SLS-" + convert(varchar(13), LastNumber)
    from LastSalesNumber

Here the problem may be the reverse: "gaps" in your sales numbers.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
Comment Utility
The problem is atomicity, which can be solved using transactions:

begin transaction
/* Assign Sales number to be last sales number assigned + 1  */

select @SalesNumber = "SLS-" + convert(varchar(13), LastNumber + 1)
    from LastSalesNumber


/*  increment the last Sales number assigned  */

update LastSalesNumber
    set LastNumber = LastNumber + 1

commit transaction


This will make that if a first user is inside this transaction, any other user will have to wait until that one has finished. Using your code, they can immediately go on...

Cheers
0
 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
Unfortunately with this code there is nothing to stop two spids getting the same sales number as

select @SalesNumber = "SLS-" + convert(varchar(13), LastNumber + 1)
   from LastSalesNumber

will take a shared lock which is compatible with another shared lock and the lock will be released immediately.
Code that will work is

update LastSalesNumber
set LastNumber = LastNumber + 1 ,
@SalesNumber = "SLS-" + convert(varchar(13), LastNumber + 1)

or
begin tran
update LastSalesNumber
   set LastNumber = LastNumber + 1

select @SalesNumber = "SLS-" + convert(varchar(13), LastNumber)
from LastSalesNumber
commit tran

commit transaction
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
update LastSalesNumber
   set LastNumber = LastNumber + 1

select @SalesNumber = "SLS-" + convert(varchar(13), LastNumber)
   from LastSalesNumber

This doesn't work either without the transaction as two spids could do the update before both getting the same number.
0
 
LVL 1

Author Comment

by:geowilli
Comment Utility
Thanks for the answer and brief explanation
0
 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
um.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now