I'm using locks and transactions a bit like a VBA excel programmer trying to write a multithreaded c++ server for the first time...
I've tried to ask my coworkers for advice, but while we're all quite good (or so we think) at designing complex databases, writing fast and efficient queries, using index and constraints when it's needed, and so on, none of us has a good knowledge of this topic.
All the online resources I've found are either syntaxical references, or dummy tutorials explaining that a transaction begins with 'begin tran' and ends with a commit or with a rollback.
I've browsed SO too without success.
What I'm looking for is a list of simple real world problems, along with the right way to solve them.
Example :
Let's say I've got a table with one Active bit column, and that I don't want to have two active rows at the same time. Of course, many processes can try to insert data at the same time.
- should I lock the whole table ?
- or maybe use a data constraint so that an insert of a second "Active" row will fail ?
- or use a transaction with the repeatable read isolation level ?
or maybe write :
update tbFoo set Active=0 insert into tbFoo (foo, Active) select 'foo',1 where not exists (select * from tbFoo where Active=1)
Please don't comment/answer on this specific problem and on my silly suggestions. I'm just trying to pinpoint the fact that I don't have a clue :)
Where can I find some good walkthroughs on simple yet relevant locking situations? If it makes a difference, I'm using SQL Server 2008
I'm also curious of knowing if other people feel the same way I do on this topics.