Answered by:
Concurrency and deadlocks...what should i do

Question
-
Hello forums,
Ive been reading a bit on concurrency and deadlocks while i doubt i understand it 100% i thhink i got the general basics. But according to my reading material (
Microsoft® SQL Server® 2008 T-SQL Fundamentals (PRO-Developer) [Paperback])
The Default isolation leel is READCOMMITED and i can set the lock time out to 5 seconds so if a deadlock occurs one transaction is killed...etc etc
but the thing is thats all i can see that needs to be done but according to my reading, dead locks and concurrency problems are one of the worst thing that could happen to a database. But i dont know what else to do cause according to my untrained eye everything has already been accounted for. So I was just looking for any general tips or stuff to look out for. Thanks
- Changed type Naomi N Thursday, April 19, 2012 1:22 PM Question rather than discussion
Thursday, April 19, 2012 12:54 PM
Answers
-
What do you mean by "what should I do"?
If you are asking about what to take in mind to avoid deadlocks, then:
- Use short transactions
- Access resources in the same order
- Use the lower isolation level needed for your task
- Avoid user interaction in transactions
- Use a row versioning-based isolation level
Minimizing Deadlocks
http://msdn.microsoft.com/en-us/library/ms191242.aspxHere is a serie of blog posts, by Bart Duncan, about deadlock troubleshooting.
http://blogs.msdn.com/b/bartd/archive/tags/sql+deadlocks/
Here is a serie of blog posts, by Sunil Agarwal, about concurrency.
http://blogs.msdn.com/b/sqlserverstorageengine/archive/tags/concurrency/
AMB
- Proposed as answer by Naomi N Thursday, April 19, 2012 4:40 PM
- Marked as answer by Kalman Toth Monday, October 8, 2012 5:55 PM
Thursday, April 19, 2012 2:04 PM -
If 10 users from accounting entering financial transactions (like $100,000 payment), rain or shine, you have to make sure that none of the transactions lost due to inferior concurrency control design .... or you will get a call from the CFO-s secretary for a very unpleasant meeting with the CFO ....
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Proposed as answer by Naomi N Thursday, April 19, 2012 4:40 PM
- Edited by Kalman Toth Monday, October 8, 2012 5:55 PM
- Marked as answer by Kalman Toth Monday, October 8, 2012 5:56 PM
Thursday, April 19, 2012 3:51 PM -
But also it seems to me that locks and probably some deadlocks cannot be avoided
Probably so. For fast, smooth operating OLTP system, the tables must be designed narrow and fixed column width, the T-SQL transactions must be quick and efficient.
Read at my article on OPTIMISTIC concurrency. Basically you "peek" before UPDATE to see if anything changed (rowversion column) since reading that record. Alternative is PESSIMISTIC concurrency control.
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked as answer by Stephanie Lv Friday, April 27, 2012 5:27 AM
- Edited by Kalman Toth Monday, October 8, 2012 5:54 PM
Thursday, April 19, 2012 5:21 PM
All replies
-
Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
Thursday, April 19, 2012 12:57 PMAnswerer -
Consider OPTIMISTIC concurrency control using rowversion (timestamp formerly) column:
http://www.sqlusa.com/articles2005/rowversion/
Deadlock prevention: http://www.sqlusa.com/bestpractices/deadlock/
Kalman Toth SQL SERVER & BI TRAINING
- Edited by Kalman Toth Thursday, April 19, 2012 2:23 PM
Thursday, April 19, 2012 1:19 PM -
why wouldnt just setting lock limit to 5 seconds solve everything?
Thursday, April 19, 2012 1:37 PM -
why wouldnt just setting lock limit to 5 seconds solve everything?
Would your company's president be overly happy if you drop a $100,000 transaction?
Instead of mechanical solution, you have to focus on the business scenario. You have to make sure that your solution makes business sense.
Kalman Toth SQL SERVER & BI TRAINING
Thursday, April 19, 2012 1:59 PM -
What do you mean by "what should I do"?
If you are asking about what to take in mind to avoid deadlocks, then:
- Use short transactions
- Access resources in the same order
- Use the lower isolation level needed for your task
- Avoid user interaction in transactions
- Use a row versioning-based isolation level
Minimizing Deadlocks
http://msdn.microsoft.com/en-us/library/ms191242.aspxHere is a serie of blog posts, by Bart Duncan, about deadlock troubleshooting.
http://blogs.msdn.com/b/bartd/archive/tags/sql+deadlocks/
Here is a serie of blog posts, by Sunil Agarwal, about concurrency.
http://blogs.msdn.com/b/sqlserverstorageengine/archive/tags/concurrency/
AMB
- Proposed as answer by Naomi N Thursday, April 19, 2012 4:40 PM
- Marked as answer by Kalman Toth Monday, October 8, 2012 5:55 PM
Thursday, April 19, 2012 2:04 PM -
ok well ive been reading this book and there was an issue where the select couldnt run because of an exclusive lock on the same table. The companies president would not be mad if he was doing a select and it failed because of an update which takes ... what...a second?
he can just re-run his query after and get thru...
right?
Thursday, April 19, 2012 2:20 PM -
Correct.
The company's president would be sligthly annoyed if a query takes too long.
But the same company's president would be real mad if you drop a $100,000 transaction due to concurrency problems.
Kalman Toth SQL SERVER & BI TRAINING
Thursday, April 19, 2012 2:25 PM -
how will the transaction drop,,, all he has to do is try again when the first transaction releases the lock or hopefully it doent get deadlocked
Also the best isolation level is read commited which is already the default in SQL- Edited by Kemnet Thursday, April 19, 2012 2:30 PM
Thursday, April 19, 2012 2:28 PM -
I meant if an employee from accounting was entering a $100,000 transaction and the system dropped it due to insufficient concurrency design.
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Edited by Kalman Toth Monday, October 8, 2012 5:55 PM
Thursday, April 19, 2012 2:36 PM -
ok, when you say insufficient cncurrency design what do you mean, is it the cases where two tracsactions have a lock on a table but they need to select the record from each other to continue?
Thursday, April 19, 2012 2:41 PM -
If 10 users from accounting entering financial transactions (like $100,000 payment), rain or shine, you have to make sure that none of the transactions lost due to inferior concurrency control design .... or you will get a call from the CFO-s secretary for a very unpleasant meeting with the CFO ....
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Proposed as answer by Naomi N Thursday, April 19, 2012 4:40 PM
- Edited by Kalman Toth Monday, October 8, 2012 5:55 PM
- Marked as answer by Kalman Toth Monday, October 8, 2012 5:56 PM
Thursday, April 19, 2012 3:51 PM -
ok but doesnt an exclusive lock already take care of that?Thursday, April 19, 2012 4:12 PM
-
Looking at the alexander_kuznetsov examples, they all show deadlocks example but not how to over come them.
But also it seems to me that locks and probably some deadlocks cannot be avoided
- Edited by Kemnet Thursday, April 19, 2012 4:37 PM
Thursday, April 19, 2012 4:35 PM -
But also it seems to me that locks and probably some deadlocks cannot be avoided
Probably so. For fast, smooth operating OLTP system, the tables must be designed narrow and fixed column width, the T-SQL transactions must be quick and efficient.
Read at my article on OPTIMISTIC concurrency. Basically you "peek" before UPDATE to see if anything changed (rowversion column) since reading that record. Alternative is PESSIMISTIC concurrency control.
Kalman Toth SQL SERVER 2012 & BI TRAINING
New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012- Marked as answer by Stephanie Lv Friday, April 27, 2012 5:27 AM
- Edited by Kalman Toth Monday, October 8, 2012 5:54 PM
Thursday, April 19, 2012 5:21 PM -
ok i shall study this an repost in a few weeks , tanksThursday, April 26, 2012 2:59 AM