Hi,
>>> when I debugged the program,I found two running threads reach the 5th line same time, A) which means they both obtained the exclusive locks of the same record ? B) Didn't the get() method obtain the exclusive lock?
LockMode.UPGRADE obtains a database level lock, if that is supported.
Oracle and PostgreSQL support it. I don't know about other databases.
Further, in this scenario ( I simulated on Oracle, Kindly excuse! I don't have SQL server. )
Both the transactions obtain exclusive lock of the same record, but in turns.
NOT AT THE SAME TIME.
Here, one transaction waits for the exclusive lock to be released by the other.
Thus, we see two running threads at the same time.
This is valid and expected.
>>> how to lock the hole db table ?
Hibernate doesn't support table level locks.
We can obtain them using jdbc, if the underlying database supports them.
-----------------------------------------------
Rate the reply if you find it helpful
|