Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 13th, 2006, 08:17 AM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default Pessimistic locking

I am attempting to try a pesimitic lock, meaning that i want to lock a row or table for a period of time and then relase it when i am done. To test this i wrote the following but it is not locking.

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
BEGIN TRANsaction
Select * From configurationitem WITH (ROWLOCK,xlock)
where name = 'NextReceiptNumber' and category = 'AR';

Declare @i int
set @i = 0
while @i < 300000
Begin
     print @i
     set @i = @i + 1
end
COMMIT TRANsaction


To test, while the above is looping i open another query windows and select from the same table using the following:

Select ConfigurationItemValue From configurationitem where ItemID = 418

This does not work because this query returns IMMEDIATELY. However, if I change the query to the following:

Select ConfigurationItemValue From configurationitem where name = 'NextReceiptNumber' and category = 'AR';

It does not return until the first query above is finished (which is the way it should work).

So, my question is, why does it not lock when i select by a primary key but lock when i do NOT select by a primary key (ItemID is a primary key).

thanks in advance.
__________________
========================
Scott Klein
Author of:
Professional SQL Server 2005 XML
Professional WCF Programming: .NET Development with the Windows Communication Foundation
Professional LINQ
========================
 
Old September 14th, 2006, 12:51 PM
SQLScott's Avatar
Wrox Author
 
Join Date: Dec 2004
Posts: 338
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Just as an FYI, we found out that if you do an UPDATE first, then the select, the row is locked as we would expect. The downside to this method (and it is not a bad downside) is that since we are goign the process backwards, (update THEN select vs. select then update) we have to subtract 1 from the value we select. No big deal, but we do get the results we want.






Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO vs DAO pessimistic locking knowledge76 Access VBA 3 December 3rd, 2008 12:17 PM
Database Locking royalsurej General .NET 2 November 9th, 2004 08:24 AM
Table Locking hortoristic SQL Server 2000 1 February 9th, 2004 09:17 PM
Pessimistic locking Skittle VB Databases Basics 1 November 17th, 2003 08:43 AM
Locking Peter Riley SQL Server ASP 3 June 5th, 2003 07:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.