Link to home
Start Free TrialLog in
Avatar of BlueAlien
BlueAlien

asked on

Transaction blocking while accesing a stored procedure SQL2000

Hi.

I have an application (in Delphi and using ADO) which looks like this:
-----------------------------------------------------------------------------------------------------------------
procedure Func;
Connection.IsolationLevel:=ilReadCommitted;
Connection.BeginTrans;
...
while(some condition)
begin
...
tmp:=GetNextSN;
...
end;
...
Connection.CommitTrans;

function GetNextSN: String;
var dsGetNextSN: TADODataSet;
begin
  dsGetNextSN.CommandText:='get_next_sn';
  dsGetNextSN.Open;
  dsGetNextSN.Close;
end;

Where 'get_next_sn' is a stored procedure:
CREATE PROCEDURE [get_next_sn]
AS  
BEGIN
  SELECT next_sn FROM table1;
  UPDATE table1 SET next_sn=next_sn+1;
END
-----------------------------------------------------------------------------------------------------------------

The problem is, that when I run multiple instances of this app, they block each other while accessing the 'get_next_sn' stored procedure. The first one reaching it runs, while the others wait until it commits.

What I would need, is to block multiple access to the SP, so that the returned 'next_sn' values are unique. Any suggestions?

Thanks.
Avatar of Titan522
Titan522

Why are you using a table to store the value? Almost any database will have something to give you an increasing number that is thread safe
Avatar of BlueAlien

ASKER

I'm using the table because I didn't know that there are similar functions, could you tell me the name of that function? I'm using MSSQL2000. But I dont need that number for inserting a primary key or something like that and it cannot be random!
In Sql there is a identity column. Create a coulmn of type int bigint smallint and towards the bottom when you are designing the table there is a idenity option.

http://www.databasejournal.com/features/mssql/article.php/3307541
I don't need this number to insert rows! I just need a function which returns numbers in ascending order for every call.
I finally solved the problem. I generate the numbers outside the transaction. Which has more logic because the numbers cannot be rolled back.
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial