MySQL optimistic lock summary and practice

Source: Internet
Author: User

The previous article, "MySQL pessimistic lock summary and practice" talked about the MySQL pessimistic lock, but the pessimistic lock is not suitable for any scenario, it also has some shortcomings of its existence,

Because pessimistic locks are implemented in most cases by the lock mechanism of the database, to guarantee the maximum degree of exclusivity of the operation. If the lock time is too long, other users can not access for a long time, affecting the program's concurrent access,

This also has a large impact on database performance overhead, especially for long transactions, which are often unsustainable. So, in contrast to pessimistic locking, we have an optimistic lock, see below for details:

Introduction to optimistic Locking:

Optimistic lock (optimistic Locking) relative pessimistic lock, optimistic locking hypothesis that the data generally do not cause conflict, so when the data is submitted to update the data will be formally conflicting or not detected, if a conflict is found, then let the return of the user error information, Let the user decide how to do it. So how do we achieve optimistic locking, there are generally the following 2 ways:

1. Using the data version recording mechanism is implemented, which is the most common way to implement optimistic locking. What is a data version? is to add a version identifier to the data, typically by adding a "version" field of a numeric type to the database table. When the data is read, the value of the version field is read together, and the data is updated each time, adding one to this version value. When we submit an update, the current version of the corresponding record of the database table is judged to be compared with the first fetch, if the current version number of the database table is equal to the first one taken out, it is updated, otherwise it is considered to be outdated data. Use one of the following diagrams to illustrate:

As shown, if the update operation is executed sequentially, the version of the data is incremented sequentially, and no conflict occurs. However, if there are different business operations to modify the same version of the data, then the first commit operation (Figure B) will update the data version to 2, when a after B commits the update found that the data version has been modified, then A's update operation will fail.

2. The second way to achieve optimistic locking is similar to the first one, as well as adding a field to the table that requires optimistic lock control, the name does not matter, the field type uses the timestamp (timestamp), and the above version is like The time stamp of the data in the current database is also checked at the time of the update submission, and the timestamp is compared before the update, if the consistency is OK, otherwise it is the version conflict.

Examples of Use : MySQL InnoDB as an example

Or take the previous example to lift: the commodity goods table has a field Status,status 1 for the product is not under the order, status is 2 for the product has been put orders, then we have to order a product to ensure that the product status is 1. Suppose the product has an ID of 1.

The order operation consists of 3 steps:

1. Check out the product informationSelect(Status,status,version) fromT_goodswhereId=#{id}2generate orders based on product information.3. Modify the item status to 2UpdateT_goodsSetStatus=2, version=Version+1whereId=#{id} and version=#{version};

So in order to use optimistic lock, we first modify the T_goods table, add a version field, the data default version value is 1.

T_goods table initial data is as follows:

So in order to use optimistic lock, we first modify the T_goods table, add a version field, the data default version value is 1.

Mysql> Select *  fromT_goods; +----+--------+------+---------+  |Id|Status|Name|Version|  +----+--------+------+---------+  |  1 |      1 |Props|       1 |  |  2 |      2 |Equipment|       2 |  +----+--------+------+---------+  2Rowsinch SetMySQL>  

For the implementation of optimistic locking, I use MyBatis to practice, specifically as follows:

Goods entity classes:

/*** Classname:goods <br/> * Function: Commodity entity. <br/> * date:2013-5-8 Morning 09:16:19 <br/> *@author[email protected]*/ Public classGoodsImplementsSerializable {/*** Serialversionuid: Serialization ID. */    Private Static Final Long  serialversionuid = 6803791908148880587L; /*** ID: primary key ID. */    Private intID; /*** Status: Commodity: 1 No orders, 2 orders. */    Private intstatus; /*** Name: Product name. */    PrivateString name; /*** Version: Product data version number. */    Private intversion; @Override PublicString toString () {return"Good ID:" +id+ ", goods Status:" +status+ ", Goods Name:" +name+ ", goods version:" +version; }    //Setter and Getter}

Goodsdao

/**@author@param @return*/int Updategoodsusecas (Goods Goods);

Mapper.xml

<update id= "Updategoodsusecas" parametertype= "Goods" >    <!  [cdata[        update t_goods        set status=#{status},name=#{name},version=version+1        where ID =#{id} and version=#{version}    ]]></update>

Goodsdaotest Test class

@Test Public voidgoodsdaotest () {intGoodsid = 1; //Query the product information according to the same ID, assign to 2 objectsGoods GOODS1 = This. Goodsdao.getgoodsbyid (GOODSID); Goods Goods2= This. Goodsdao.getgoodsbyid (GOODSID); //Print Current Product informationSystem.out.println (GOODS1);        System.out.println (GOODS2); //Update product Information 1Goods1.setstatus (2);//Modify status to 2    intUPDATERESULT1 = This. Goodsdao.updategoodsusecas (GOODS1); System.out.println ("Modify product Information 1" + (updateresult1==1? ") Success ":" Failed ")); //Update product Information 2Goods1.setstatus (2);//Modify status to 2    intUPDATERESULT2 = This. Goodsdao.updategoodsusecas (GOODS1); System.out.println ("Modify product Information 2" + (updateresult2==1? ") Success ":" Failed "));}

Output Result:

Good id:1,goods status:1,goods Name: props, goods version:1good ID:1,goods status:1,goods Name: props, goods version:1 Modify product information 1 successfully modified product information 2 failure

Description

In the Goodsdaotest test method, we also identify the same version of the data, assign to different goods objects, and then modify the Good1 object and then perform the update operation, the execution succeeds. Then we modify the GOODS2, and the prompt operation fails when the update operation is performed. At this point the data in the T_goods table is as follows:

Mysql> Select *  fromT_goods;+----+--------+------+---------+|Id|Status|Name|Version|+----+--------+------+---------+|  1 |      2 |Props|       2 ||  2 |      2 |Equipment|       2 |+----+--------+------+---------+2Rowsinch SetMySQL> 

We can see that the data version with ID 1 has been modified to 2 on the first update. So when we update good2, the update where condition already does not match, so the updates will not succeed, the specific SQL is as follows:

Updateset status=2, version=version+1 where id= and version=#{version};

So we can achieve an optimistic lock.

MySQL optimistic lock summary and practice

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.