The locking mechanism in hibernate

Source: Internet
Author: User

One: What is a lock?

There is a need for mechanisms to ensure that the data is not modified by the outside world during an operation, such as the so-called "lock" that locks the target data (objects) that we have selected so that it cannot be modified by other programs.

Two: The lock in hibernate?

Hibernate supports two locking mechanisms-"pessimistic lock (pessimistic Locking)"-"optimistic lock (optimistic Locking)".

Three: How to use the lock?

Pessimistic lock: Pessimistic lock just like his name, he thought that every time he was working on the database, other client programs were doing the same thing. As a result, the data of the operation is locked until the operation is complete and unlocked.

Therefore, the implementation of pessimistic locks is generally dependent on the database itself lock mechanism.

Optimistic Lock: Optimistic locking provides a looser locking mechanism relative to pessimistic locks. He was optimistic that he would rarely have the same situation when working with the data. He is not locked at the database level. In order to maintain the correct data, the logical implementation of the optimistic lock application data version number control resolution.

Data version number: A version ID is added to maintain the database, typically by adding a "version" field to the database table.

Pessimistic lock implementation is based on the lock mechanism of the database, you can use the Setlockmode () method of query or criteria to set the data to be locked and locked mode.

For example:

Query query=session.createquery ("from User u where u.name= ' Kitty '");

Query.setlockmode (' C ', lockmode.upgrade);

List list=query.list ();

The code sends a user object with an alias of "U" locked, and the lock mode in Hibernate is:

-Lockmode.none: No lock mechanism

-LockMode.WRITE:Hibernate lock on insert and update record

-LockMode.READ:Hibernate Lock when reading a record

-Lockmode.upgrade: Use the database's for UPDATE clause to lock.

-Lockmode. Specific implementations of Upgrade_nowait:oracle, using Oracle's for UPDATE NOWAIT clauses for locking

Note: Locking is only really done through the lock mechanism of the database until the query starts (that is, before the hiberate generates SQL), otherwise the data has been loaded through a select SQL that does not contain a FOR UPDATE clause, so the so-called database lock is meaningless.

Optimistic Lock:

Optimistic locking relies on the data version recording mechanism to implement

-Add a version ID to the data

-Read the version number together when reading the data

-When updating, version number plus one

-Compare the version of the submitted data with the current version of the database table corresponding to the record

-If the submitted data version number is greater than the current version number of the database table, it is updated, otherwise it is considered to be outdated data

First: Add a version property to the persistence class and the corresponding method.

Next, add the Optimistic-lock attribute to the class descriptor:

For example:

< hibernate-mapping >

<class name= "Com.cy.beans.Homework" table= "T_homework" catalog= "java ee" optimistic-lock = "Version" >

......

</class >

The Optimistic-lock property resembles the following selectable value:

-None No optimistic lock

-Version Optimistic Lock (official recommendation) via release mechanism

-Dirty optimistic locking by checking for changed properties

-All enable optimistic locking by checking all properties

Then: Add a Version Property descriptor

< version column = "version" name = "Version" type = "Java.lang.Integer"/>

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.