How database locking mechanism works
I am sorry if I am asking the same question again. I tried finding
information about database locking and at last I am confused. I am using
Syabse ASE and I performed the following
Connected to sybase from 3 terminals and began a transaction from each
connection.
Transactions T1, T2, T3.
T1 and T2 acquires a share lock by using
lock table sample in share mode
T1 and T2 are performing select operations
T3 comes and tries to update sample table for which it needs a exclusive
lock. Since I have used
set lock wait 60
T3's update statement will be killed after 60 seconds.
Now T1 tries to update the sample table for which it needs an exclusive
lock. Since T2 is also holding a shared lock on sample table, T1 will hold
now. Since T1 does not have any wait time out it will wait for indefinite
time for T2 to commit the transaction.
Now T2 also tries to update the sample table for which it needs an
exclusive lock. Since T1 is also holding a shared lock on sample table
sybase will detect a dead lock and kill T2's update statement and then
T1's update statement is executed successfully.
Assuming that the transaction which requested the update first will
complete successfully and the other transaction's statement will be killed
by sybase.
Now T2 again tries to update the sample table. T2 is kept on hold
indefinitely.
Now T1 again tries to update the sample table. This time T1's update
statement will be finished successfully and T2 is still waiting.
This is where I got confused.
T2 has first requested for the update. This time T1 should be killed if
there is a dead lock.
I assume that T1 has now got an exclusive lock on sample table
automatically when it first updated the table.
Can a transaction's lock be automatically changed?
Then later I executed a select from T1 and it worked fine. Assuming that
exclusive lock will be released and changed to shared.
Next I executed an update from T1. T1 finished again and T2 is still waiting.
Should I explicitly change the lock to shared in T1?
I did this. I tried changing the lock to shared by using
lock table sample in share mode
Then I got this message
Table 'sample' in database 'shmdb' was not locked in 'shared' mode because
a covering lock in 'shared' mode was found.
Now I checked T2's status. Its still waiting.
Then I tried to update sample table from T1 it ran fine and T2 is still
waiting.
Too much confusion.
How can I change the lock on T1? Committing the transaction will release
the locks and T2 will run fine.
Is this the only possible way?
No comments:
Post a Comment