Optimistic locks and pessimistic locks of hibernate-pessimistic locks

Source: Internet
Author: User

Locking)
During the implementation of business logic, data access needs to be exclusive. For example, end computing on the day of the Financial System
Processing, we want to process the data at a cut-off time point, rather than in the settlement process.
(Several seconds or several hours), and the data changes again. In this case, we need to use some machines
This mechanism ensures that the data is not modified by the outside world during an operation.
Lock, that is, lock the target data we have selected so that it cannot be modified by other programs.
Hibernate supports two lock mechanisms: Pessimistic locking )"
And "Optimistic Locking )".
Pessimistic locking)
Pessimistic lock, just like its name, refers to external data (including other current transactions of the system, as well
The transaction processing of the external system.
Status. Pessimistic locks are implemented based on the locks provided by the database (and only the locks provided by the database layer can be implemented.
Data access is exclusive. Otherwise, even if the lock mechanism is implemented in the system, external systems cannot be guaranteed.
Does not modify data ).
A typical pessimistic lock call that relies on databases:
Select * from account where name = "Erica" for update
This SQL statement locks all records that meet the search criteria (name = "Erica") in the account table.
Before the transaction is committed (the lock in the transaction process will be released when the transaction is committed), these records cannot be modified by the outside world.
The pessimistic lock of Hibernate is also implemented based on the database lock mechanism.
The following code locks query records:

String hqlstr =
"From Tuser as user where user. Name = 'erica '";
Query query = session. createquery (hqlstr );
Query. setlockmode ("user", lockmode. Upgrade); // lock
List userlist = query. List (); // execute the query to obtain data.
Query. setlockmode: locks the records corresponding to a specific alias in the query statement.
The Tuser class specifies an alias "user"). Here, all user records returned are locked.
Observe the SQL statements generated by hibernate during runtime:
Select tuser0 _. ID as ID, tuser0 _. Name as name, tuser0 _. group_id
As group_id, tuser0 _. user_type as user_type, tuser0 _. Sex as sex
From t_user tuser0 _ Where (tuser0 _. Name = 'erica ') for update
Here, Hibernate implements the pessimistic lock mechanism by using the for update clause of the database.
The locking modes of hibernate include:
Ø lockmode. None: No lock mechanism.
Ø lockmode. Write: hibernate will automatically insert and update records
.
Ø lockmode. Read: hibernate will automatically obtain the data when reading the record.
The above three lock mechanisms are generally used internally by hibernate, for example, to ensure update
During the process, the object will not be modified by the outside world, and the write lock will be automatically added to the target object in the implementation of the Save method.
Ø lockmode. Upgrade: locks the database by using the for update clause.
Ø lockmode. upgrade_nowait: specific implementation of Oracle, using the Oracle
Update Nowait clause implements locking.
The above two locking mechanisms are commonly used at the application layer. The locking is generally implemented through the following methods:
Criteria. setlockmode
Query. setlockmode
Session. Lock
Note that the lock is set only before the query starts (that is, before the hiberate generates the SQL statement ).
The database lock mechanism is used to lock the data. Otherwise, the data has passed for update
When the select SQL statement of the clause is loaded, the so-called database locking is impossible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.