472,952 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Autonumbering causing deadlocks.

Gents,

I have come into a system that uses a secondary table to generate (for
want of a better word) Identities.

eg

create table myidents
( name sysname not null, ident int not null)

create procedure getnextident @table sysname, @ident int output
as
begin
if not exists (select top 1 1 from myidents where name = @table)
insert into myidents values (@table, 0)

update myidents
set @ident = ident = ident + 1
where name = @table
end

now, (ignoring for now the use of reserved words) the problem is that
this is called frequently, from other procedures. Trouble is that the
calling procedures call it from within a transaction. We now have a
wickedly hot spot on this table, with frequent deadlocks.

Is there any relatively quick fix for this? Some locking hints or
whatever.

Or do we need to go and recode, moving this kind of thing outside the
transaction (which are all rather too long for my liking), and even
cosidering using identity columns as a replacement?

Thanks
Jul 20 '05 #1
4 2217
IMHO, IDENTITY would be the best approach with the current design. You
might also consider using natural keys instead of surrogate key values.

If your actual myident table has no primary key on name, this will
contribute to blocking/deadlocking. Unless your application always acquires
values in the same sequence, you will be vulnerable to deadlocks unless you
specify a TABLOCKX hint. This is obviously bad for concurrency (especially
for long-running transactions) and should be avoided if possible.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"WangKhar" <Wa******@yahoo.com> wrote in message
news:bb**************************@posting.google.c om...
Gents,

I have come into a system that uses a secondary table to generate (for
want of a better word) Identities.

eg

create table myidents
( name sysname not null, ident int not null)

create procedure getnextident @table sysname, @ident int output
as
begin
if not exists (select top 1 1 from myidents where name = @table)
insert into myidents values (@table, 0)

update myidents
set @ident = ident = ident + 1
where name = @table
end

now, (ignoring for now the use of reserved words) the problem is that
this is called frequently, from other procedures. Trouble is that the
calling procedures call it from within a transaction. We now have a
wickedly hot spot on this table, with frequent deadlocks.

Is there any relatively quick fix for this? Some locking hints or
whatever.

Or do we need to go and recode, moving this kind of thing outside the
transaction (which are all rather too long for my liking), and even
cosidering using identity columns as a replacement?

Thanks

Jul 20 '05 #2
You could try this:

if not exists (select top 1 1 from myidents ROWLOCK, UPDLOCK where name =
@table)
Hope it will help.

Igor

"WangKhar" <Wa******@yahoo.com> wrote in message
news:bb**************************@posting.google.c om...
Gents,

I have come into a system that uses a secondary table to generate (for
want of a better word) Identities.

eg

create table myidents
( name sysname not null, ident int not null)

create procedure getnextident @table sysname, @ident int output
as
begin
if not exists (select top 1 1 from myidents where name = @table)
insert into myidents values (@table, 0)

update myidents
set @ident = ident = ident + 1
where name = @table
end

now, (ignoring for now the use of reserved words) the problem is that
this is called frequently, from other procedures. Trouble is that the
calling procedures call it from within a transaction. We now have a
wickedly hot spot on this table, with frequent deadlocks.

Is there any relatively quick fix for this? Some locking hints or
whatever.

Or do we need to go and recode, moving this kind of thing outside the
transaction (which are all rather too long for my liking), and even
cosidering using identity columns as a replacement?

Thanks

Jul 20 '05 #3
WangKhar (Wa******@yahoo.com) writes:
create procedure getnextident @table sysname, @ident int output
as
begin
if not exists (select top 1 1 from myidents where name = @table)
insert into myidents values (@table, 0)

update myidents
set @ident = ident = ident + 1
where name = @table
end

now, (ignoring for now the use of reserved words) the problem is that
this is called frequently, from other procedures. Trouble is that the
calling procedures call it from within a transaction. We now have a
wickedly hot spot on this table, with frequent deadlocks.

Is there any relatively quick fix for this? Some locking hints or
whatever.


Deadlocks can usually be avoided by:

BEGIN TRANSACTION

SELECT @ident = ident FROM tbl (UPDLOCK) WHERE name = @table
UPDATE tbl SET ident = @ident WHERE name = @table

COMMIT TRANSACTION

However, if you have more work in the transaction, you will have lot of
blocking and not much concurrency in the system. And if you in some
procedures perform work before you come here, they might be blocking
other processes that aleady have an ident, and now wil block the other
prcoess.

The above is only really meaningful if you have busienss rules that
require consecutive series. Else you should move out the ident-generation
out of the transaction or use IDENTITY instead.
--
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
Thanks thats pretty much what I thought ...

See if I can convince people that we need to take this action :(
Erland Sommarskog <so****@algonet.se> wrote in message news:<Xn*********************@127.0.0.1>...
WangKhar (Wa******@yahoo.com) writes:
create procedure getnextident @table sysname, @ident int output
as
begin
if not exists (select top 1 1 from myidents where name = @table)
insert into myidents values (@table, 0)

update myidents
set @ident = ident = ident + 1
where name = @table
end

now, (ignoring for now the use of reserved words) the problem is that
this is called frequently, from other procedures. Trouble is that the
calling procedures call it from within a transaction. We now have a
wickedly hot spot on this table, with frequent deadlocks.

Is there any relatively quick fix for this? Some locking hints or
whatever.


Deadlocks can usually be avoided by:

BEGIN TRANSACTION

SELECT @ident = ident FROM tbl (UPDLOCK) WHERE name = @table
UPDATE tbl SET ident = @ident WHERE name = @table

COMMIT TRANSACTION

However, if you have more work in the transaction, you will have lot of
blocking and not much concurrency in the system. And if you in some
procedures perform work before you come here, they might be blocking
other processes that aleady have an ident, and now wil block the other
prcoess.

The above is only really meaningful if you have busienss rules that
require consecutive series. Else you should move out the ident-generation
out of the transaction or use IDENTITY instead.

Jul 20 '05 #5

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

Similar topics

3
by: Fredrik M?ller | last post by:
Hi All, I have read about deadlocks here on Google and I was surprised to read that an update and a select on the same table could get into a deadlock because of the table's index. The update...
2
by: Tim McAuley | last post by:
Hi, I have a stored procedure that is causing deadlocks when called multiple times synchronously. The odd issue is that the deadlock seems to be happening on different threads waiting for locks...
9
by: Mike Carr | last post by:
I am running into an issue. Recently I installed IBuySpy Portal and then converted the data source to odp.net. When debugging the app my machine would freeze or become really slow. I can reproduce...
16
by: Ben | last post by:
I'm doing a bunch of data mining against a postgres database and have run into an interesting problem with deadlocks. The problem is, postgres is detecting them and then wacking the offending...
4
by: Carlos Moreno | last post by:
Hello, I'm using PostgreSQL 7.4.3 on a RedHat 9 Linux server (a P4 HyperThreaded, using the SMP kernel, in case this makes a difference). I'm not 100% sure I understand exactly why I am...
6
by: Greg Stark | last post by:
There's another poster complaining about referential integrity checks causing deadlocks. Unfortunately I've deleted the message so this response (and the archives aren't responding) isn't going to...
2
by: Rahul Babbar | last post by:
Hi, Is there any way to find the SQL that is causing the Locks. I am using SYSPROC.SNAPSHOT_LOCK to find out the locks on the table, but i am unable to find out which SQL statement was...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.