The locking mechanism in hibernate

Source: Internet
Author: User

Locking System: is the database in order to ensure consistency of data < A transaction of various operations do not affect each other > to make a variety of shared resources in the concurrency access is ordered by the design of a rule, to ensure that the current user to manipulate the data at the time of the other users can not do anything to the same data.

Hibernate is a persistent layer framework that is used to access data in the database, and hibernate provides its own locking mechanism to ensure data consistency.

Hibernate's lock mechanism:

Optimistic Lock: <pessimistic locking> He thinks that it is generally not possible for multiple users to operate the same data at the same time, so do not lock on the database level, only do the application layer logical implementation of the solution.

Pessimistic Lock: <optimistic locking> to make the worst of the plan, that is, he thinks that every time the data is operational there will always be other users to operate the same data, so the entire data must be locked to ensure that each operation is correct, until the completion of the operation unlocked.

"Hibernate has a solution with inconsistent data without pessimistic locking:

1: First update to Primary

2: Post-update main

3: Check for changes in the data, or check all properties to achieve optimistic locking.

Implementation of optimistic locks implemented in Hibernate:< update to main > by version number

1. A database table, which is different from the previous one. Attribute version (version number) 

1 CREATE TABLE User (2     ID INT (one) not NULL auto_increment PRIMARY KEY,3 c7>    versions INT,--version number attribute 4     name VARCHAR ("default",  5Age    INT6 );

2 Build entity class, same as one attribute version (version number)

 Public classUser {PrivateInteger ID; PrivateInteger version;//increase the version of the    PrivateString name; PrivateInteger age;  PublicUser () {} PublicInteger getId () {returnID; }     Public voidsetId (Integer id) { This. ID =ID; }     PublicInteger getversion () {returnversion; }     Public voidsetversion (Integer version) { This. Version =version; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }       PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }}

3 configuration file construction

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE hibernate-mapping    "-//hibernate/hibernate mapping DTD 3.0//en"    "http// Hibernate.sourceforge.net/hibernate-mapping-3.0.dtd ">class  optimistic-lock= "Version"><!--plus optimistic lock-        <id name= "id" >        class= "Native"/>        </id>        <version name= "version"/>        <property name= "name"/>        <property name= " Age "/> </class>

Version number must be mapped after the ID.

The version number is updated each time the data is updated, and when the first object is updated, the version number changes when the two same objects are obtained. When the second object is updated again, a version number error is found, and an exception update failure is thrown. An exception can be caught to handle this object effectively.

The disadvantage of optimistic locking: Based on the version number to achieve the lock, rather than using the database lock mechanism, easily I cross the version number < such as force manually modify the data version number > operation data, resulting in the entire lock mechanism failure.

  The realization of pessimistic lock in hibernate:< through the function of system and database itself to realize >

Implement the table or column you want to lock and its locking mode by using query or criteria Setlockmode ()     

      • Lockmode.upgrade: Use the FOR update clause of the repository for locking.
      • lockmode.upgrade_nowait: Use the for update NOWAIT clause to lock and use in the Oracle repository.

Session session = Sessionfactory.opensession ();

Query query = Session.creatquery ("from User U");

    Query.setlockmode ("U", lockmode.upgrade);//Set Lock mode

The following three locking modes hibernate are automatically locked internally, regardless of the database

      • lockmode.write: When an insert or update is locked ,hibernate locks automatically when the save () method is used.
      • Lockmode.read: Hibernate will automatically get locked when reading records.
      • Lockmode.none: No lock.

The disadvantage of pessimistic locking: in the network of concurrent access of multiple users, the database lock will inevitably lead to long waits for users, greatly reducing the speed of data access, and user experience.

The locking mechanism in hibernate

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.