views:

210

answers:

3

Hi guys,

I am thinking this is impossible but I wanted to make sure.

Is there a way for me to know when a table was locked and maybe for how long? I know that I can see whether a table is currently locked, but I would like to have a "history" of locks.

+2  A: 

Check this tool out from Red-Gate. I use it an awful lot for exactly this kind of thing. Plus it lets me check out long running queries and a host of other useful information. There are filters for Last hour, last day, forever etc...

RedGate SQL Response

Pace
+3  A: 

A "free" alternative to the RedGate tool mentioned in other response, is the MS-SQL Server Profiler (see in Tools menu from SQL Server Management Studio). This tool lets you define "traces" by specifying the type of event you wish to monitor and/or record. There's even [in SQL2008, maybe also in older versions] a default template for locking issues: *TSQL_Locks*.

Beware that analyzing the profiler's logs may require a bit of work/figuring out. It is possible to filter events based on a particular set of criteria (as well as filtering these at the source, i.e. excluding these from the log in the first place), but third party products such as RedGate's are likely to offer more ease-of-use, better aggregation features etc.

Edit:
(following Metju's remark) The solution suggested above, implies that one would start recording lock-related events in the profiler tool ahead of time. There is nothing, at least nothing publicly documented, in MS-SQL 2005 which would provide access to a complete historical info about the locks, "post facto". However, depending on one's needs, enough insight may sometimes be gathered from the SQL Activity Monitor (from 'Management' in the databases etc. tree on the left, in Management Studio), in particular the "Locks by object" view. This information, which is implicitly available (no need for any setup), can often be sufficient to identify the origins of dead-locks and similar issues.

mjv
So the only way is for me to create my own audit and have a record from now on, there is no built in audit in 2005?I actually need this because something came up and we need want to know what happened.Thanks a lot
Metju
Be wary, though--you can get a LOT of "hits" when tracking locking activity.
Philip Kelley
Right-on, Phillip, I was thinking along the same lines when I added a small paragraph about filtering and limited aggregation features.
mjv
@Metju - that's correct. Lock history is not maintained natively anywhere in SQL Server.
Chris J
@Chris J. Thank you, for confirming the lack of implicit/natively maintained historical data. @Metju, see edit in answers, with suggestion to look into the `SQL Server Activity Monitor` (if you haven't readily done so) to maybe identify some info in there that would, at least in part, provide you insight into what happened.
mjv
I don't know if I could add this here instead of posting another question:We realised something that is rather strange.What we are doing is creating a unique value, inserting into a table and then get it from that table and use it as a cookie - if this doesn't work we have a backup value.The strange thing is, we've read some of the cookies and were unique(as they should be) but found no insertion for it in the table. Quite bizarre, not sure this has anything to do with locking any more though.
Metju
+1  A: 

Without running a monitoring tool (a roll your own, third party, or processing the output of SQL profiler), no there isn't.

Chris J