I have a problem where it seems that when running the same stored procedure at the same time from two different instances of the same application it is timing out, and wondered if there was anything I could do to resolve it?
I believe the problem is in the way SQL Server 2008 handles itself, locking rows and executing the SPs…things I don’t really know a whole lot about. The app uses ADODB.Command to execute the SP.
I have one VB6 exe (App.exe), running on the one server multiple times. This app calls a stored proc on the database which returns the next sequence number for that app. This sequence number field is unique for the instance of the application – there is 1 row in a table (tbl_SequenceNos) for each instance of the running application.
So for example, say we have running: App[a].exe and App[b].exe
tblSequenceNos looks like:
iAppNo| iNextSequenceNo
a | 1234
b | 4567
The stored procedure to get the next sequence number is relatively simple:
CREATE PROEDURE GetNextSequenceNo (@AppNo varchar(1), @NextSequenceNo int output)
AS
BEGIN
DECLARE @TempSequenceNo int
SELECT @NextSequenceNo = iNextSequenceNo
FROM tblSequenceNos
WHERE iAppNo = @AppNo
@NextSequenceNo = @NextSequenceNo + 1
UPDATE tblSequenceNos
SET iNextSequenceNo = @NextSequenceNo
WHERE iAppNo = @AppNo
END
When both App[a].exe and App[b].exe try to run this procedure to get their NextSequenceNo value, they are hanging for about 30Secs (ADO timeout?).
Because Each app never looks at the each others row, I thought that this would work concurrently without specifing an special Locking. Is there something I am missing? I thought perhaps I need to specify to lock the row only, not the whole table or Page? – I do not know what sql2008 does by default.
Any help is greatly appreciated!
Thank you in advance
Andrew
Your procedure is not thread safe and will produce incorrect results because between the select and the update multiple threads might get the same sequence nr.
Also make sure your iAppNo column is indexed. (This means an index on this column only or an index where this field is the first field in your index)