Hello All!
I want to perform 4 or 5 statements as a transaction but I need to make sure that during this complete transaction no one else inserts or deletes records from a table named SomeTable.
So how can I lock MyTable at the beggining of the transaction so that during my transaction no one else can insert or delete anything in table SomeTable?
Thanks!
David
You can open an transaction explicitly so other transactions are placed in a queue. Use BEGIN TRAN /COMMIT TRAN. Read up books on line for more info on transactions.
|||Yup, I could read about this as you suggest, but it's certainly pretty complex so I was hoping someone experienced and knowleadgable would give me the answer.
Your post but it doesn't answer my question. I know how to create an explicit transaction.
My question is how do I lock a table during a transaction so that no one else inserts or deletes records from it.
I don't think that by just creating an explicit transaction and reading a row from MyTable this will lock the complete table.
|||
neutrino:
I don't think that by just creating an explicit transaction and reading a row from MyTable this will lock the complete table.
Yes it does. And thats all you have to do. Try this scenario:
(1) In a query analyzer window run this script
BEGIN TRAN
INSERT INTO TheTable ... <complete the rest of the INSERT>
(2) Open another query analyzer and do a SELECT * From TheTable. You will see that your query will be in "suspended" status waiting for the Insert to finish since you opened a Transaction explicitly.
(3) Now go back to the first window and run this:
Commit
(4) Check the second window and you will see results for your SELECT *...
Basically your BEGIN TRAN has opened a transaction --> locked the table for any other commited transactions (Even reads). As soon as you commit the transaction the lock on the table is released and others can read from the table. You can by pass this and do a dirty read by using NOLOCK hint. Not always suggested unless your business requirements allow you to.
|||
Thanks. This tells me how to lock a table by creating a transaction and doing and insert statement. However, in my scenario I need to lock the table innitially before doing any inserts to it and I want it to remain locked until the transaction ends (even if I don't do any inserts). I don't want any other transaction to be able to insert any rows until my transaction finishes.
I think that what I need is to set the transaction isolation level to SERIALIZABLE.
I found this: http://msdn2.microsoft.com/en-us/library/ms173763.aspx
I'll be trying it later and will post results.
David
|||
I do find your query analyzer excercise extremelly useful. I will use it to test my locking 'theories". Thanks!
|||
ndinakar:
neutrino:
I don't think that by just creating an explicit transaction and reading a row from MyTable this will lock the complete table.
Yes it does. And thats all you have to do. Try this scenario:
(1) In a query analyzer window run this script
BEGIN TRAN
INSERT INTO TheTable ... <complete the rest of the INSERT>
(2) Open another query analyzer and do a SELECT * From TheTable. You will see that your query will be in "suspended" status waiting for the Insert to finish since you opened a Transaction explicitly.
(3) Now go back to the first window and run this:
Commit
(4) Check the second window and you will see results for your SELECT *...
Basically your BEGIN TRAN has opened a transaction --> locked the table for any other commited transactions (Even reads). As soon as you commit the transaction the lock on the table is released and others can read from the table. You can by pass this and do a dirty read by using NOLOCK hint. Not always suggested unless your business requirements allow you to.
Actually, that doesn't lock the whole table. It locks a portion of the table, but your SELECT requires access to the entire table (including the locked portion), so it has to wait. If you have the table indexed, and the SELECT can use the index to determine that it doesn't need the locked portion then it won't delay the SELECT. In addition, a second INSERT should complete without being delayed.
To the original poster, what you are asking for is rather uncommon, and you are best to try and avoid doing what you are asking to do. Perhaps you need to rethink why you want the table locked, and what you are trying to accomplish by doing so. Usually there is a much better way of achieving that.
|||
Motley:
To the original poster, what you are asking for is rather uncommon, and you are best to try and avoid doing what you are asking to do. Perhaps you need to rethink why you want the table locked, and what you are trying to accomplish by doing so. Usually there is a much better way of achieving that.
You are totally right. This was actually what I did. I re-thought the process and found a better way that doesn't require the table lock.
But anyways it was a great learning experience.
Thanks all for your support.
David
|||
ndinakar:
neutrino:
I don't think that by just creating an explicit transaction and reading a row from MyTable this will lock the complete table.
Yes it does. And thats all you have to do. Try this scenario:
It actually doesn't but the rest of your post was really helpful. Thank you.
No comments:
Post a Comment