Showing posts with label view. Show all posts
Showing posts with label view. Show all posts

Wednesday, March 21, 2012

Deadlock on a select query, possible?

I have a simple select query that selects data from a view. I consistently get a deadlock exception when running this query:

Server: Msg 1205, Level 13, State 2, Line 1
Transaction (Process ID #) was deadlocked on thread | communication buffer resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

The view is a simple select statement that has WITH (NOLOCK) as a hint on all tables. I thought I understand how deadlocks worked, two threads are holding a lock and request the other's item. How does a read uncommitted select statement participate in this?

If I look at the other processes in the current activity and run sql profiler, there is no other activity and no existing locks at the time the statement is run.

Can anyone explain this? Or should I bounce my server and hope it never happens again?

Thanks,

DaveAlthough the (NOLOCK) hint is puzzling, it is possible to deadlock on a single SELECT. I replicated this behavior about 12 years ago, but can't remember the specific scenario other than it was happening on a wide table (I think there was one row to a page, 2K pages) and the deadlock happened on the index.

To see where the deadlock is occuring, you may want to try:
Connection 1:
DBCC TraceOn(1204)
go

Begin Tran
Select foo From v_bar WITH (NOLOCK)
go

Connection 2 (immediately after executing 1)
Exec sp_lock
go

Check the errorlog to see the results of the trace and read up on Troubleshooting Deadlocks in SQL BOL.

Good luck.

Wednesday, March 7, 2012

DDL Trigger

SQL 2005
I have a table that is referenced by a view. Whenever a column is added or
dropped from a table I want to update the view to include or remove the
column - so I thought, 'DDL Trigger!'
I constructed my trigger and immediately found that the Alter Table is not
committed at the time the trigger is run - the trigger is in the same
transaction as the Alter Table - so the column does not exist yet.
What I am looking for is an AFTER DDL Trigger. Until then I guess I am off
to figure out Notification Services.In this case, a DDL trigger -- in my mind at least -- is for logging and/or
rolling back the change to the table.
What were you expecting to do to the table directly while in the scope of
the trigger?
"Joe L" <jjj@.lll.com> wrote in message
news:etW5zLJOGHA.3888@.TK2MSFTNGP12.phx.gbl...
> SQL 2005
> I have a table that is referenced by a view. Whenever a column is added
> or dropped from a table I want to update the view to include or remove the
> column - so I thought, 'DDL Trigger!'
> I constructed my trigger and immediately found that the Alter Table is not
> committed at the time the trigger is run - the trigger is in the same
> transaction as the Alter Table - so the column does not exist yet.
> What I am looking for is an AFTER DDL Trigger. Until then I guess I am
> off to figure out Notification Services.
>
>|||I was going to dynamically update my view to include/remove the column that
was just added or dropped.
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:udXODOJOGHA.3276@.TK2MSFTNGP09.phx.gbl...
> In this case, a DDL trigger -- in my mind at least -- is for logging
> and/or rolling back the change to the table.
> What were you expecting to do to the table directly while in the scope of
> the trigger?
>
>
> "Joe L" <jjj@.lll.com> wrote in message
> news:etW5zLJOGHA.3888@.TK2MSFTNGP12.phx.gbl...
>