I ‘m writing a windows application which connects to sql server and performs a update action on a table.
Already there is another program named P2 which is altering the data in same table. So the table is locked
I am getting an error while accessing the table from my application
I need a solution in which the program first checks if the table is already locked and if the table is not locked the data has to be updated.
If the table is already locked then it has to wait for a while and retry the operation.
Can anyone Provide me one??
It is never a good idea to check if something is locked or not before performing an operation. Because a lock can be acquired just after you perform the check and just before you perform your update:
So it’s futile to try to check the lock. You should go ahead and try to acquire the lock instead and perform your update right away. You could be avoiding that because the other operation takes too long and you don’t want user to wait. In that case you could specify a “short lock wait timeout” and provide user a message saying that “try again later”. User doesn’t need to wait.