In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
What is a pessimistic lock? In view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Pessimistic locking refers to a conservative attitude towards data being modified by the outside world, that is, in the process of data processing, the data is locked, and this function needs to rely on the locking mechanism provided by the database, otherwise, even if the locking mechanism is implemented in the system, there is no guarantee that the external system will not modify the data.
Lock mode
LockMode.NONE
Unlocked mechanism
LockMode.READ
Hibernate automatically acquires locks when reading records
That is, shared lock: (Shared lock, S lock), shared lock is also called read lock. If transaction T acquires a shared lock on data object A (that is, adding a shared lock to A), then other transactions can only acquire a shared lock on A (S lock), not an exclusive lock (X lock). Until A releases all shared locks. Transactions that are allowed to share locks can only read data and cannot modify it.
LockMode.WRITE
Hibernate automatically acquires locks when insert acquires update records
That is, exclusive lock: (Exclusive lock, X lock), exclusive lock is also called write lock. If transaction T acquires an exclusive lock on data A, T can both read and write A, but other transactions can acquire neither a shared lock on A nor an exclusive lock on A until T releases the X lock on A.
LockMode.UPGRADE
If the database system supports pessimistic locks (such as Oracle and MySQL), execute select... For update statements (row-level locking, on which other transactions cannot make update, insert, and delete statements), and if the database does not support pessimistic locks (such as Sybase), execute normal select statements.
LockMode.UPGRADE_NOWAIT
It has the same function as LockMode.UPGRADE. In addition, execute select for the Oracle database. For update nowait statement. "nowait" means that if the transaction executing the select statement does not immediately acquire a pessimistic lock, it will not wait for another transaction to release the lock, but will immediately throw a lock exception.
Demonstration of lock
Note: locks work only on data objects that operate in a single transaction, not on the entire database.
Open two SQL command operation line interfaces, which can represent two transactions T1 and T2. Let's first execute the command in two interfaces: start transaction
Query the student information with student number "2015" in the database in transaction T1 and add a pessimistic lock to the operational data: select * from stu_info where stu_no= "2015" for update;. At this point, we can query the corresponding information.
"for update" means adding a pessimistic lock. The transaction is not committed after this query, that is to say, the lock has not been released
Query the student information with student number "2016" in the database in transaction T2 and add a pessimistic lock to the operational data: select * from stu_info where stu_no= "2016" for update;. At this time, we can also query the corresponding information.
The transaction has not been committed yet
Query the student information with student number "2015" in the database in transaction T2. This time, do the ordinary select query without locking: select * from stu_info where stu_no= "2015";. At this time, we can also query the corresponding information.
The transaction has not been committed yet
In transaction T2, try to query the student information in the database with student number "2015" and add a pessimistic lock to the operating data: select * from stu_info where stu_no= "2015" for update;. At this point, we can find that the relevant information is not displayed, but is waiting. When we commit transaction T1 (that is, commit), the data object "2015" releases the pessimistic lock in T1 so that T2 transaction can query the relevant information and acquire the pessimistic lock.
If we change step 5. In the fifth step just now, we modified the student number "2016" in transaction T1 but did not add a pessimistic lock, because we added a pessimistic lock to the record in transaction T2 before it was not released, so, the modification can be successful only after T2 commits.
The answer to the question about what is pessimistic lock is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.