Showing posts with label encountering. Show all posts
Showing posts with label encountering. Show all posts

Wednesday, March 21, 2012

Deadlock on simple update related to clustered index

I am encountering a deadlock situation, with two threads
running two updates to the same table, if that table has a
clustered index on it that is NOT the primary key, and the
where clause of the UPDATE statement is on the primary
key. Very strange... Here is the test case:
create table x
(id int not null,
lastModified datetime not null,
altkey varchar(3) not null)
GO
CREATE UNIQUE CLUSTERED INDEX IAK_X ON x (altKey)
GO
alter table x add constraint pk_x primary key(id)
GO
insert into x
values (1, getdate(), 'XXX');
Then, from 2 ISQL threads, ran the following code...
begin tran
waitfor time '15:06' -- ensure they start at same time
update x
set lastModified = getdate()
where id = 1;
waitfor delay '000:00:10' -- wait to ensure lock occurs
update x
set lastModified = getdate()
where id = 1;
commit tran;
When I monitor the locks, the following happens:
Thread A successfully performs Update 1
Thread B is blocked as A has Exclusive lock on the row.
Thread B, however, successfully gets a KEY UPDATE lock on
PK_X
Thread A, when it tries to perform Update 2, is blocked
trying to get a KEY UPDATE lock on PK_X, causing the
deadlock.
Even if I then drop the clustered index with
DROP INDEX x.iak_x;
the problem remains.
However, if I NEVER create the clustered index. The
threads both work fine and never encounter a deadlock.
Neither transaction attempts to get a KEY UPDATE lock on
the primary key index PK_X.
We try to use clustered indexes on fields other than the
ID # (one-up number, but NOT an IDENTITY column in this
case), because we encountered deadlocks on Inserts in the
past.
Any help would be greatly appreciated.On Thu, 24 Jul 2003 14:02:58 -0700, "Corey" <chorton1@.austin.rr.com>
wrote:
>Any help would be greatly appreciated.
That's pretty humorous.
I've never gotten into the detailed guts of SQLServer like some around
here, but it sounds to me like you've hit a SQLServer bug. It looks
like SQLServer locks the data record in thread A, but allows thread B
to begin and get a lock on an index entry while waiting for the data,
and voila, deadlock. If that is the case, I'd call it a bug.
I assume it works correctly if you have the PK on id?
J.sql

Deadlock issue with 1204 results

I am consistently encountering deadlocks while running an app, with
replication running. My transactional replication insert proc always wins,
but my app fails. Below I have included the information from a 1204 trace,
but I don't really know how to read it. I believe that spid 63 has a page
level intent exclusive lock on my table, and a select statement spid 60
Share lock. I know these locks are incompatible, and this is the issue.
How do I find out more information on how to fix this?
What else should I look at?
How do I read the log results below?
2004-01-30 14:04:19.52 spid4 Wait-for graph
2004-01-30 14:04:19.52 spid4
2004-01-30 14:04:19.52 spid4 Node:1
2004-01-30 14:04:19.52 spid4 PAG: 9:3:671699 CleanCnt:1
Mode: IX Flags: 0x2
2004-01-30 14:04:19.52 spid4 Grant List 0::
2004-01-30 14:04:19.52 spid4 Owner:0x2cccf7c0 Mode: IX Flg:0x0
Ref:1 Life:02000000 SPID:63 ECID:0
2004-01-30 14:04:19.52 spid4 SPID: 63 ECID: 0 Statement Type: INSERT
Line #: 8
2004-01-30 14:04:19.52 spid4 Input Buf: RPC Event:
sp_MSins_transactions;1
2004-01-30 14:04:19.52 spid4 Requested By:
2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: S
SPID:60 ECID:0 Ec:(0x29ECB550) Value:0x560354e0 Cost:(0/0)
2004-01-30 14:04:19.52 spid4
2004-01-30 14:04:19.52 spid4 Node:2
2004-01-30 14:04:19.52 spid4 TAB: 9:983674552 [] CleanCnt:1
Mode: S Flags: 0x0
2004-01-30 14:04:19.52 spid4 Grant List 2::
2004-01-30 14:04:19.52 spid4 Owner:0x56012440 Mode: S Flg:0x0
Ref:1 Life:00000001 SPID:60 ECID:0
2004-01-30 14:04:19.52 spid4 SPID: 60 ECID: 0 Statement Type: SELECT
INTO Line #: 44
2004-01-30 14:04:19.52 spid4 Input Buf: Language Event: goto byacct--
/*
delete from exceptions where account_number = '32022238' and symbol = 'ADCT'
delete from matched_transaction where account_number = '32022238'and symbol
= 'ADCT'
delete from unmatched_transaction where account_number = '32022238'and
2004-01-30 14:04:19.52 spid4 Requested By:
2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: IX
SPID:63 ECID:0 Ec:(0x6DE8F590) Value:0x2cccfee0 Cost:(0/514)
2004-01-30 14:04:19.52 spid4 Victim Resource Owner:
2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: S
SPID:60 ECID:0 Ec:(0x29ECB550) Value:0x560354e0 Cost:(0/0)
2004-01-30 14:04:19.52 spid60 Error: 1205, Severity: 13, State: 61
2004-01-30 14:04:19.52 spid60 Transaction (Process ID 60) was deadlocked
on lock resources with another process and has been chosen as the deadlock
victim. Rerun the transaction..Captainkt
I assume you know what is a deadlock.
Do you have error handler in your applications with some delay which does
not give to user to wait until SQL Server would kill the victim?
Here are some tips on how to avoid deadlocking on your SQL Server:
Ensure the database design is properly normalized.
Have the application access server objects in the same order each time.
During transactions, don't allow any user input. Collect it before the
transaction begins.
Avoid cursors.
Keep transactions as short as possible. One way to help accomplish this is
to reduce the number of round trips between your application and SQL Server
by using stored procedures or keeping transactions with a single batch.
Another way of reducing the time a transaction takes to complete is to make
sure you are not performing the same reads over and over again. If you do
need to read the same data more than once, cache it by storing it in a
variable or an array, and then re-reading it from there.
Reduce lock time. Try to develop your application so that it grabs locks at
the latest possible time, and then releases them at the very earliest time.
if appropriate, reduce lock escalation by using the ROWLOCK or PAGLOCK.
Consider using the NOLOCK hint to prevent locking if the data being locked
is not modified often.
If appropriate, use as low of an isolation level as possible for the user
connection running the transaction.
Consider using bound connections.
"captainkt" <nothing@.fake.com> wrote in message
news:uPjQmma6DHA.2720@.TK2MSFTNGP09.phx.gbl...
> I am consistently encountering deadlocks while running an app, with
> replication running. My transactional replication insert proc always
wins,
> but my app fails. Below I have included the information from a 1204
trace,
> but I don't really know how to read it. I believe that spid 63 has a page
> level intent exclusive lock on my table, and a select statement spid 60
> Share lock. I know these locks are incompatible, and this is the issue.
> How do I find out more information on how to fix this?
> What else should I look at?
> How do I read the log results below?
> 2004-01-30 14:04:19.52 spid4 Wait-for graph
> 2004-01-30 14:04:19.52 spid4
> 2004-01-30 14:04:19.52 spid4 Node:1
> 2004-01-30 14:04:19.52 spid4 PAG: 9:3:671699 CleanCnt:1
> Mode: IX Flags: 0x2
> 2004-01-30 14:04:19.52 spid4 Grant List 0::
> 2004-01-30 14:04:19.52 spid4 Owner:0x2cccf7c0 Mode: IX
Flg:0x0
> Ref:1 Life:02000000 SPID:63 ECID:0
> 2004-01-30 14:04:19.52 spid4 SPID: 63 ECID: 0 Statement Type:
INSERT
> Line #: 8
> 2004-01-30 14:04:19.52 spid4 Input Buf: RPC Event:
> sp_MSins_transactions;1
> 2004-01-30 14:04:19.52 spid4 Requested By:
> 2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: S
> SPID:60 ECID:0 Ec:(0x29ECB550) Value:0x560354e0 Cost:(0/0)
> 2004-01-30 14:04:19.52 spid4
> 2004-01-30 14:04:19.52 spid4 Node:2
> 2004-01-30 14:04:19.52 spid4 TAB: 9:983674552 [] CleanCnt:1
> Mode: S Flags: 0x0
> 2004-01-30 14:04:19.52 spid4 Grant List 2::
> 2004-01-30 14:04:19.52 spid4 Owner:0x56012440 Mode: S
Flg:0x0
> Ref:1 Life:00000001 SPID:60 ECID:0
> 2004-01-30 14:04:19.52 spid4 SPID: 60 ECID: 0 Statement Type:
SELECT
> INTO Line #: 44
> 2004-01-30 14:04:19.52 spid4 Input Buf: Language Event: goto
byacct--
> /*
> delete from exceptions where account_number = '32022238' and symbol ='ADCT'
> delete from matched_transaction where account_number = '32022238'and
symbol
> = 'ADCT'
> delete from unmatched_transaction where account_number = '32022238'and
> 2004-01-30 14:04:19.52 spid4 Requested By:
> 2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: IX
> SPID:63 ECID:0 Ec:(0x6DE8F590) Value:0x2cccfee0 Cost:(0/514)
> 2004-01-30 14:04:19.52 spid4 Victim Resource Owner:
> 2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: S
> SPID:60 ECID:0 Ec:(0x29ECB550) Value:0x560354e0 Cost:(0/0)
> 2004-01-30 14:04:19.52 spid60 Error: 1205, Severity: 13, State: 61
> 2004-01-30 14:04:19.52 spid60 Transaction (Process ID 60) was
deadlocked
> on lock resources with another process and has been chosen as the deadlock
> victim. Rerun the transaction..
>
>

Deadlock issue with 1204 results

I am consistently encountering deadlocks while running an app, with
replication running. My transactional replication insert proc always wins,
but my app fails. Below I have included the information from a 1204 trace,
but I don't really know how to read it. I believe that spid 63 has a page
level intent exclusive lock on my table, and a select statement spid 60
Share lock. I know these locks are incompatible, and this is the issue.
How do I find out more information on how to fix this?
What else should I look at?
How do I read the log results below?
2004-01-30 14:04:19.52 spid4 Wait-for graph
2004-01-30 14:04:19.52 spid4
2004-01-30 14:04:19.52 spid4 Node:1
2004-01-30 14:04:19.52 spid4 PAG: 9:3:671699 CleanCnt:1
Mode: IX Flags: 0x2
2004-01-30 14:04:19.52 spid4 Grant List 0::
2004-01-30 14:04:19.52 spid4 Owner:0x2cccf7c0 Mode: IX Flg:0x0
Ref:1 Life:02000000 SPID:63 ECID:0
2004-01-30 14:04:19.52 spid4 SPID: 63 ECID: 0 Statement Type: INSERT
Line #: 8
2004-01-30 14:04:19.52 spid4 Input Buf: RPC Event:
sp_MSins_transactions;1
2004-01-30 14:04:19.52 spid4 Requested By:
2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: S
SPID:60 ECID:0 Ec0x29ECB550) Value:0x560354e0 Cost0/0)
2004-01-30 14:04:19.52 spid4
2004-01-30 14:04:19.52 spid4 Node:2
2004-01-30 14:04:19.52 spid4 TAB: 9:983674552 [] CleanCnt:1
Mode: S Flags: 0x0
2004-01-30 14:04:19.52 spid4 Grant List 2::
2004-01-30 14:04:19.52 spid4 Owner:0x56012440 Mode: S Flg:0x0
Ref:1 Life:00000001 SPID:60 ECID:0
2004-01-30 14:04:19.52 spid4 SPID: 60 ECID: 0 Statement Type: SELECT
INTO Line #: 44
2004-01-30 14:04:19.52 spid4 Input Buf: Language Event: goto byacct--
/*
delete from exceptions where account_number = '32022238' and symbol = 'ADCT'
delete from matched_transaction where account_number = '32022238'and symbol
= 'ADCT'
delete from unmatched_transaction where account_number = '32022238'and
2004-01-30 14:04:19.52 spid4 Requested By:
2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: IX
SPID:63 ECID:0 Ec0x6DE8F590) Value:0x2cccfee0 Cost0/514)
2004-01-30 14:04:19.52 spid4 Victim Resource Owner:
2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: S
SPID:60 ECID:0 Ec0x29ECB550) Value:0x560354e0 Cost0/0)
2004-01-30 14:04:19.52 spid60 Error: 1205, Severity: 13, State: 61
2004-01-30 14:04:19.52 spid60 Transaction (Process ID 60) was deadlocked
on lock resources with another process and has been chosen as the deadlock
victim. Rerun the transaction..Captainkt
I assume you know what is a deadlock.
Do you have error handler in your applications with some delay which does
not give to user to wait until SQL Server would kill the victim?
Here are some tips on how to avoid deadlocking on your SQL Server:
Ensure the database design is properly normalized.
Have the application access server objects in the same order each time.
During transactions, don't allow any user input. Collect it before the
transaction begins.
Avoid cursors.
Keep transactions as short as possible. One way to help accomplish this is
to reduce the number of round trips between your application and SQL Server
by using stored procedures or keeping transactions with a single batch.
Another way of reducing the time a transaction takes to complete is to make
sure you are not performing the same reads over and over again. If you do
need to read the same data more than once, cache it by storing it in a
variable or an array, and then re-reading it from there.
Reduce lock time. Try to develop your application so that it grabs locks at
the latest possible time, and then releases them at the very earliest time.
if appropriate, reduce lock escalation by using the ROWLOCK or PAGLOCK.
Consider using the NOLOCK hint to prevent locking if the data being locked
is not modified often.
If appropriate, use as low of an isolation level as possible for the user
connection running the transaction.
Consider using bound connections.
"captainkt" <nothing@.fake.com> wrote in message
news:uPjQmma6DHA.2720@.TK2MSFTNGP09.phx.gbl...
quote:

> I am consistently encountering deadlocks while running an app, with
> replication running. My transactional replication insert proc always

wins,
quote:

> but my app fails. Below I have included the information from a 1204

trace,
quote:

> but I don't really know how to read it. I believe that spid 63 has a page
> level intent exclusive lock on my table, and a select statement spid 60
> Share lock. I know these locks are incompatible, and this is the issue.
> How do I find out more information on how to fix this?
> What else should I look at?
> How do I read the log results below?
> 2004-01-30 14:04:19.52 spid4 Wait-for graph
> 2004-01-30 14:04:19.52 spid4
> 2004-01-30 14:04:19.52 spid4 Node:1
> 2004-01-30 14:04:19.52 spid4 PAG: 9:3:671699 CleanCnt:1
> Mode: IX Flags: 0x2
> 2004-01-30 14:04:19.52 spid4 Grant List 0::
> 2004-01-30 14:04:19.52 spid4 Owner:0x2cccf7c0 Mode: IX

Flg:0x0
quote:

> Ref:1 Life:02000000 SPID:63 ECID:0
> 2004-01-30 14:04:19.52 spid4 SPID: 63 ECID: 0 Statement Type:

INSERT
quote:

> Line #: 8
> 2004-01-30 14:04:19.52 spid4 Input Buf: RPC Event:
> sp_MSins_transactions;1
> 2004-01-30 14:04:19.52 spid4 Requested By:
> 2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: S
> SPID:60 ECID:0 Ec0x29ECB550) Value:0x560354e0 Cost0/0)
> 2004-01-30 14:04:19.52 spid4
> 2004-01-30 14:04:19.52 spid4 Node:2
> 2004-01-30 14:04:19.52 spid4 TAB: 9:983674552 [] CleanCnt:1
> Mode: S Flags: 0x0
> 2004-01-30 14:04:19.52 spid4 Grant List 2::
> 2004-01-30 14:04:19.52 spid4 Owner:0x56012440 Mode: S

Flg:0x0
quote:

> Ref:1 Life:00000001 SPID:60 ECID:0
> 2004-01-30 14:04:19.52 spid4 SPID: 60 ECID: 0 Statement Type:

SELECT
quote:

> INTO Line #: 44
> 2004-01-30 14:04:19.52 spid4 Input Buf: Language Event: goto

byacct--
quote:

> /*
> delete from exceptions where account_number = '32022238' and symbol =

'ADCT'
quote:

> delete from matched_transaction where account_number = '32022238'and

symbol
quote:

> = 'ADCT'
> delete from unmatched_transaction where account_number = '32022238'and
> 2004-01-30 14:04:19.52 spid4 Requested By:
> 2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: IX
> SPID:63 ECID:0 Ec0x6DE8F590) Value:0x2cccfee0 Cost0/514)
> 2004-01-30 14:04:19.52 spid4 Victim Resource Owner:
> 2004-01-30 14:04:19.52 spid4 ResType:LockOwner Stype:'OR' Mode: S
> SPID:60 ECID:0 Ec0x29ECB550) Value:0x560354e0 Cost0/0)
> 2004-01-30 14:04:19.52 spid60 Error: 1205, Severity: 13, State: 61
> 2004-01-30 14:04:19.52 spid60 Transaction (Process ID 60) was

deadlocked
quote:

> on lock resources with another process and has been chosen as the deadlock
> victim. Rerun the transaction..
>
>
sql

Monday, March 19, 2012

Deadlock detection problem! help

Hello faculties,

i'm encountering a strange a deadlock problem on my remote server
which is used to give application demo to the client. It has happened
that on one of the databases a deadlock situation is taking place.

What is the most detailed way to detect such the cause of such a
deadlock to the innermost level of detail, like what statements, stored
procedures and locks are causing the deadlock to occur.

Guys! please help me out!

Thanks in advance
Debian

*** Sent via Developersdex http://www.developersdex.com ***debian mojo (debian_mojo@.yahoo.com) writes:
> i'm encountering a strange a deadlock problem on my remote server
> which is used to give application demo to the client. It has happened
> that on one of the databases a deadlock situation is taking place.
> What is the most detailed way to detect such the cause of such a
> deadlock to the innermost level of detail, like what statements, stored
> procedures and locks are causing the deadlock to occur.

DBCC TRACEON (1204, 1)
DBCC TRACEON (3605, 1)

It's common to add these as start up parameters (best done from Enterprise
Manager).

This gives you a deadlock trace to the SQL Server log. Unfortunately,
though, it's a bit cryptic.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp