all groups > sql server programming > march 2005 >
You're in the

sql server programming

group:

what will be really locked by a holdlock?


what will be really locked by a holdlock? nonno
3/31/2005 10:57:02 PM
sql server programming: hi, I have a few questions about holdlock, e.g.
begin tran
....
select @last_val = max(col1) from table1 (HOLDLOCK) where col2 = 'X'
....
insert table1 (col1, col2) values (@last_val + 1, 'X')
....
commit tran
What will be locked here (the entire table1 or only the rows with col2='X') ?
Will it be locked until the transaction completed?
This program is running in a multiuser environment, I want to restrict that
only 1 session at a time can insert a row into table1 with a particular value
of col2, e.g. if session1 try to insert a row in table1 with col2='A' while
session2 inserting a row with col2='A', session1 will be blocked until
session2 transaction completed; if session1 try to insert a row in table1
with of col2='B' while session2 inserting a row with col2='A', session1 will
RE: what will be really locked by a holdlock? John Bell
4/1/2005 12:07:03 AM
Hi

From Books online:

HOLDLOCK - Hold a shared lock until completion of the transaction instead of
releasing the lock as soon as the required table, row, or data page is no
longer required. HOLDLOCK is equivalent to SERIALIZABLE.

For lock compatibility see
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/acdata/ac_8_con_7a_8um1.asp

John

[quoted text, click to view]
Re: what will be really locked by a holdlock? Wayne Snyder
4/1/2005 9:02:29 AM
The scope of the lock is determined by the plan that the optimizer
chooses.... If the optimizer chooses row level locks, then only the row will
be locked, however the optimizer may choose a page level lock, and all rows
on the page will be locked for the duration of the transaction....

--
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)

I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org

[quoted text, click to view]

Re: what will be really locked by a holdlock? nonno
4/3/2005 12:47:03 AM
thanks Wayne. You said something will be locked for the duration of the
transaction, how about nesting transaction? Will that be locked for the
duration of the nested transaction only or the entire transaction?

[quoted text, click to view]
Re: what will be really locked by a holdlock? Tibor Karaszi
4/3/2005 12:00:10 PM
Nesting transaction only exist in syntax, not semantics. The outer BEGIN TRAN begins the
transaction. When @@TRANCOUNT is zero, the transaction is ended. BEGIN TRAN increases nesting level
with 1, COMMIT decreases with 1 and ROLLBACK brings nesting level to 0.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]

Re: what will be really locked by a holdlock? nonno
4/3/2005 8:25:01 PM
Thanks Tibor. Can I release the lock before commit tran? How to do that? It's
too long to wait for the whole transaction to commit!

[quoted text, click to view]
Re: what will be really locked by a holdlock? nonno
4/3/2005 8:25:03 PM
Thanks Tibor. Can I release the lock before commit tran? How to do that? It's
too long to wait for the whole transaction to commit!

[quoted text, click to view]
Re: what will be really locked by a holdlock? Tibor Karaszi
4/4/2005 12:59:06 PM
[quoted text, click to view]

No. Keep the transactions as short as possible, make sure you have proper indexes to minimize the
amount of data needed to be locked and if needed you can do dirty reads (see for instance the NOLOCK
hint).

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/


[quoted text, click to view]

AddThis Social Bookmark Button