Hibernate optimistic lock and pessimistic lock usage

Source: Internet
Author: User

Hibernate supports two lock mechanisms: Pessimistic Locking and OptimisticLocking )". Pessimistic locks often rely on the locks provided by the database (and only the locks provided by the database layer can truly guarantee the exclusive data access; otherwise, even if the locking mechanism is implemented in the system, it cannot be ensured that the external system will not modify the data ). Hibernate locks: Ø LockMode. NONE: No lock mechanism. Ø LockMode. WRITE: Hibernate will automatically obtain the 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, in order to ensure that objects in the Update process are not modified by the outside world, Hibernate will automatically add the WRITE lock 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: the specific implementation of Oracle. Use the Oracle for update nowait clause to implement locking. The pessimistic lock of Hibernate is also implemented based on the database lock mechanism. The following code locks query records: 1 String hqlStr = "from TUser as user where user. name = 'erica '"; 2 Query query = session. createQuery (hqlStr); 3 query. setLockMode ("user", LockMode. UPGRADE); // lock 4 List userList = query. list (); // execute the query to obtain the data query. setLockMode locks the record corresponding to the specific alias in the query statement (we have specified an alias "user" for the TUser class). In this case, all the returned user records are locked. Observe the SQL statement generated by Hibernate at runtime: 1 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 Hibernate implements a pessimistic lock mechanism by using the for update clause of the database. The above two locking mechanisms are commonly used at the application layer. The following methods are generally used for locking: Criteria. setLockMode Query. setLockMode Session. lock Note: only when the lock is set before the query starts (that is, before the Hiberate generates the SQL statement) Will the database lock mechanism be used for locking. Otherwise, data has been loaded through Select SQL that does not contain the for update clause. The so-called database locking is impossible. Optimistic locks are mostly implemented based on the data Version record mechanism. What is the data version? Add a version ID for the data. In the database table-based version solution, you can add a "version" field to the database table. When reading the data, read the version number together, and then add one to the version number when updating the data. In this case, the version data of the submitted data is compared with the current version information recorded in the database table. If the submitted data version number is greater than the current version number of the database table, it is updated, otherwise, expired data is considered. 1. first, add the optimistic-lock attribute for the TUser class descriptor:

Related Article

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.