025 Hibernate pessimistic lock, optimistic lock

Source: Internet
Author: User

Hibernate talk about pessimistic lock, optimistic lock, it is necessary to talk about the concurrency of the database, the higher the level of isolation of the database, its concurrency is worse

Concurrency: After the current system has been serialized, the current reading data, others can not query, can not see. Called Concurrency is not good

Database Isolation Level: see previous chapter level

025-1 Pessimistic Lock:

Pessimistic Lock: Has exclusivity (I lock the current data, others see this data)

Pessimistic locking is generally done by the data mechanism.

Implementation of pessimistic lock

Usually relies on the database mechanism, during the refurbishment process the data locked, no other user can read or modify (such as: Must I modify, other people to modify)

Application Scenarios for pessimistic locks:

Pessimistic lock generally suitable for short transactions more (if a data is removed and added 1, immediately released)

Long transactions occupy time (if they occupy 1 hours, then this 1 hours people will not be able to use this data), not commonly used.

Instance:

Entity class:

Public class Inventory {

Private int ItemNo;

Private String ItemName;

Private int Quantity;

Public int Getitemno () {

return ItemNo;

}

Public void Setitemno (int itemno) {

this. ItemNo = ItemNo;

}

Public String Getitemname () {

return ItemName;

}

Public void setitemname (String itemname) {

this. itemname = ItemName;

}

Public int getquantity () {

return quantity;

}

Public void setquantity (int quantity) {

this. Quantity = Quantity;

}

}

Map file:

<class name="com.wjt276.hibernate.Inventory" table="T_inventory">

<id name="ItemNo">

<generator class="native"/>

</id>

<property name="ItemName"/>

<property name="Quantity"/>

</class>

Use of pessimistic locks

If a pessimistic lock is required, it must be locked when the data is loaded, usually in the database's for UPDATE statement.

Hibernate uses load for pessimistic lock loading.

Session.load (Class arg0, Serializable arg1, Lockmode arg2) throws Hibernateexception

Lockmode : Pessimistic lock mode (generally using Lockmode.upgrade)

session = hibernateutils. GetSession ();

            tx = Session.beginTransaction ( );

            Inventory inv = (Inventory) Session.load (Inventory. Class , 1, Lockmode. UPGRADE );

            System. out . println (Inv.getitemname ());

            inv.setQuantity ( Inv.getquantity () -200);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

            session.update (INV);

            tx.commit ();

Execution Output SQL statement:

Hibernate:select Inventory0_.itemno as itemno0_0_, inventory0_.itemname as itemname0_0_, inventory0_.quantity as quantity0_0_ from T_inventory inventory0_ where inventory0_.itemno=? For update//In the SELECT statement, use pessimistic lock for the for update.

Brain Platinum

Hibernate:update t_inventory set itemname=?, quantity=? where itemno=?

Note: Other users can only read after the user releases the lock

Note: If pessimistic locks are used, then lazy (frightened loading is not valid)

025-2 optimistic Lock:

Optimistic lock: Not a lock, is a conflict detection mechanism.

Optimistic locking concurrency is better, because when I change, others change with the side.

Optimistic lock implementation: Commonly used is the version of the way (each data table has a version of the field versions, one user update data, version number +1, another user modified after +1, when the user update found that the current version number of the database and read data when the version number is inconsistent (equal to less than the database current version number) , it will not update.

Hibernate using optimistic locks requires that the items in the mapping file be configured to take effect.

Entity class:

Public class Inventory {

Private int ItemNo;

Private String ItemName;

Private int Quantity;

Private int version;//hibernate user to implement version optimistic locking, but needs to be configured in the mapping file

Public int Getitemno () {

return ItemNo;

}

Public void Setitemno (int itemno) {

this. ItemNo = ItemNo;

}

Public String Getitemname () {

return ItemName;

}

Public void setitemname (String itemname) {

this. itemname = ItemName;

}

Public int getquantity () {

return quantity;

}

Public void setquantity (int quantity) {

this. Quantity = Quantity;

}

Public int getversion () {

return version;

}

Public void setversion (int version) {

this. Version = version;

}

}

mapping files

<!--mapping entity classes, you need to include an optimistic lock property

There are several ways of optimistic-lock= "version":

-None-version-dirty-all

Also need to map the version number field after the primary key mapping

-

<class name="com.wjt276.hibernate.Inventory" table="t_inventory " optimistic-lock= " Version ">

<id name="ItemNo">

<generator class="native"/>

</id>

<version name="version"/><!-must be configured after the primary key mapping-

<property name="ItemName"/>

<property name="Quantity"/>

</class>

Export the output SQL statement:

CREATE TABLE t_inventory (ItemNo integer NOT NULL auto_increment, version integer NOT NULL, ItemName varchar (255), Quantit Y integer, primary key (ItemNo))

Note: The Added version field version , or we are to maintain, is by Hibernate to maintain.

Optimistic locking is not a concern when storing data.


025 Hibernate pessimistic lock, optimistic lock

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.