Previous | Table of Contents | Next |
If you want to override the locking behavior of the server, you may do so. It is not recommended for the faint of heart. These optimizer hints allow you to alter the behavior of SQL Server to make it act more like the archaic, non-locking database presented earlier. These options are used with the SELECT statement.
However, in some cases, applied sparingly, these hints can be useful. Place the words after the table name in your query, inside parentheses.
These optimizer hints last for the duration of the SELECT. If you want them to last for the duration of the transaction, include HOLDLOCK.
This example selects data from titles, acquiring an exclusive table lock and keeping it until the transaction completes:
begin tran select title, price from titles (TABLOCKX HOLDLOCK) [ ] commit
This query acquires an exclusive table Eœck on titles until the transaction commits. Without the lock hints, the titles table would get a shared table lock and then release it immediately after the data was read.
WARNING: Remember to enclose optimizer hints in parentheses. HOLDLOCK is permitted outside them to maintain compatibility with previous SQL Server versions. However, there is a bug that causes the server to allow anything as an optimizer hint, but interpret it as only a HOLDLOCK:begin tran select title from titles YOURMOMMASOUGLY HOLDLOCKtitle ---------------------------------- But Is It User Friendly? [ ]If you examine locks before committing this transaction, youll notice that a shared table lock was acquired on titles.
The longer you hold locks, the slower your database will run. To squeeze the most out of your system, try the following tips for speedy transactions:
Deadlock is the classic samurai at the bridge scenario. Each process has locked a page the other needs. Neither can go forward until the other goes back. Rather than wait through hurricanes for one of the samurai to give way, the server will reach out of the sky and strike one of the samurai dead. In other words, when two processes have locked one another out, one of the processes gets killed, and his transaction is rolled back.
Deadlock can only be minimized. It can never be completely avoided. Deadlock can even occur in system tables, as processes compete for space and locks. If you see a few deadlocks on your system, it is nothing to be worried about. On active systems, four deadlocks a day is probably an acceptable amount. If you see 20 or a hundred a day, though, you should try to minimize this. In an application program, the victim of deadlock (the dead samurai) will receive server error #1205, which reads, in part, Your process has been chosen as the victim of a deadlock process. At this point you have two choices. You may notify your user that deadlock occurred and that whatever she was doing was undone. This is easier, but more intrusive. The other option is to resubmit the transaction. This means you must keep track of what happened on the client side and create logic that saves the transactional details, submits it, and then resubmits and executes if a 1205 occurs.
There are lots of advanced strategies for minimizing deadlock. Here are some tips to try:
Previous | Table of Contents | Next |