Groups | Blog | Home
all groups > sql server programming > september 2003 >

sql server programming : Locking question


JXStern
9/1/2003 11:34:04 AM
On Mon, 1 Sep 2003 20:52:21 +0300, "Loukas Marinis"
[quoted text, click to view]

Oh, what you asked!

The recommended approach to most modern database usage is "optimistic"
locking, in which you do something like taking the timestamp from the
record you read and use it if and when you decide to do an update.
The server can then reject your update, saying "someone beat you to
it." This is acceptable if it happens rarely enough, and in many
cases, that is the statistical situation.

So, generally, records are not detectably locked at all!

Now, if you're an old fart like myself and still prefer to do what
they call "pessimistic" locking now and again, SQLServer does support
this, but the only way I know of detecting it, is to attempt to lock
it yourself. If your lock is issued, you got it, and if it is
rejected, someone else does. There are good and bad ways to go about
this all, of course. You could attempt the lock and release it if you
get it, but ... well, you see the tangles that result. To attempt
such a lock, do a begin trans, then do a select "with (updlock)".

Since even SQLServer actually does lock records at some point,
especially in big, fat transactions, your update detection may indeed
detect some locked records, even if they are only implicitly locked
for very short periods in most cases. Though, in general, you seldom
need to or want to know about such things.

See BOL for more details on stuff like updlock.

Best of luck,
J.
Loukas Marinis
9/1/2003 8:52:21 PM
from vb6 using unbound forms i call a stored procedures which include just a
select statement like

select * from users where userId = @userId

Always return one row.

My question is how i can determine if this record is locked regardsless of
whether it will be changed or not; just to avoid complications

I want to use this in the case another user want to edit the same record and
hence running the same procedure.



Stuey
9/2/2003 12:20:12 AM
Additionally, if you want to be able to just read the row, and are not
concerned about that data being updated by another user who currently has
the same row "locked" you can use select * from tablename (nolock) hint.

Stu

[quoted text, click to view]

Loukas Marinis
9/2/2003 8:24:27 AM
Thanx for your help. I was a bit aware of what you mention and eventually
seems that timestamp is a good solution. However i was looking for a
pessimistic solution.
A bit of background
Let's suppose there is a listview with all the users. when i click one
user, a new form is opening, the required stored procedure is running(as we
said before) and the corresponding fields are filled.

My question is: The Lock is released as soon as the stored procedure is
completed? Can i somehow define when to release the lock(in my case when i
close the form) or by other means?

[quoted text, click to view]

Anthony Faull
9/2/2003 12:26:22 PM
How to find out if a table is locked:



[quoted text, click to view]

Anthony Faull
9/2/2003 12:27:46 PM
Here's how to list all the locks on a table:

select distinct page, type, spid
from master.dbo.syslocks
where dbid = db_id()
and id = object_id('dbo.Table1')


[quoted text, click to view]

AddThis Social Bookmark Button