My work has a financial application, written in
SQL, that several users can be working on at the same time.
At some point, one user might decide to Post the batch of entries that they (and possibly other people) are currently working on.
Obviously, I no longer want any other users to <strong>add</strong>, <strong>edit</strong>, or <strong>delete</strong> entries in that batch <strong>after</strong> the <strong>Post process</strong> has been initiated.
I have already seen that I can lock <strong>all</strong> data by opening the SQL transaction the moment the Post process starts, but the process can be fairly lengthy and I would prefer not to have the Transaction open for the several minutes it might take to complete the function.
Is there a way to lock just the records that I know need to be operated on from VB.NET code?Answer1:
If you are using Oracle you would <strong>Select for update</strong> on the rows you are locking.
here is an example
SELECT address1 , city, country FROM location FOR UPDATE;Answer2:
You probably want to set an isolation level for the entire transaction rather than using <em>with (rowlock)</em> on specific tables.
Look at this page:<br /><a href="http://msdn.microsoft.com/en-us/library/ms173763.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms173763.aspx</a>
Specifically, search within it for 'row lock', and I think you'll find that READ COMMITTED or REPEATABLE READ are what you want. READ COMMITTED is the SQL Server default. If READ COMMITTED doesn't seem strong enough to you, then go for REPEATABLE READ.
<strong>Update:</strong> After reading one of your follow up posts, you definitely want repeatable read. That will hold the lock until you either commit or rollback the transaction.Answer3:
to your SQL query
<a href="http://www.sql-server-performance.com/articles/per/lock_contention_nolock_rowlock_p1.aspx" rel="nofollow">SQL Server Performance article</a>
<strong>EDIT:</strong> ok, I misunderstood the question. What you want is transaction isolation. +1 to Joel :)Answer4:
wrap it in a tran use an holdlock + updlock in the select
begin tran select * from SomeTable (holdlock,updlock) where .... processing here commit