MyBatis optimistic lock implementation to solve concurrency problems

Source: Internet
Author: User

A typical example is a bank with two operators operating the same account at the same time.
For example, A, B operators simultaneously read a balance of 1000 yuan account, a operator for the account increase of 100 yuan, b operator at the same time for the account deduction of 50 yuan, a first submitted, b after submission. The final actual account balance is 1000-50 = 950 yuan, but should be 1000+100-50=1050. This is a typical concurrency problem.

The optimistic locking mechanism solves this problem to some extent. Optimistic locking, mostly based on the data version (versions) recording mechanism implementation. What is a data version? is to add a version identity to the data, which is typically done by adding a "version" field to the database table in the version solution based on the database table.

When the data is read, the version number is read together, and then the version number is added one after the update. At this point, the version data of the submitted data is compared to the current version information of the database table corresponding to the record, and if the submitted version number is greater than the current version number of the database table, it is updated, otherwise it is considered to be outdated data.

For the example above to modify user account information, assume that there is a version field in the Account information table in the database, the current value is 1, and the Current Account balance field (balance) is $1000. Assuming that operator A is updated first, operator B is updated.
A, operator a reads it out at this time (version=1) and adds 100 (1000+100=1100) from its account balance.
b, during operator A's operation, operator B also reads this user information (version=1) and deducts 50 (1000-50=950) from its account balance.
C, operator a completed the modification work, the data version number plus one (version=2), together with the account increase balance (BALANCE=1100), submitted to the database update, at this time because the submission version of the data is larger than the current version of database records, the data is updated, The database record version is updated to 2.
D, operator B completed the operation, but also the version number plus one (version=2) attempt to submit data to the database (balance=950), but at this time than the database record version found that operator B submitted a version number of 2, the database records the current version is 2, does not meet the " The commit version must be larger than the current version of the record to perform the update "optimistic locking policy, so the submission of operator B is dismissed.
This avoids the possibility of operator B overwriting operator A's results with the results of old version=1-based data modifications.

Operator a operates as follows:

SelectID, balance, version fromAccountwhereId="1"; Query Result: ID=1, balance= +, version=1Update AccountSetBalance=Balance+ -, version=Version+1 whereId="1" andVersion=1SelectID, balance, version fromAccountwhereId="1"; Query Result: ID=1, balance=1100, version=2


Operator B operates as follows:

SelectID, balance, version fromAccountwhereId="1"; Query Result: ID=1, balance= +, version=1#操作员A已修改成功, actual account.balance=1100, account.version=2, operator B also adds a version number=2) attempts to submit data to the database (balance=950), but when compared to the database record version, operator B submits a version number of 2, the current version of the database record is also 2, does not meet the "commit version must be greater than the current version of the record to perform the update" optimistic locking policy, therefore, the submission of operator B is dismissed. Update AccountSetBalance=Balance- -, version=Version+1 whereId="1" andVersion=1 SelectID, balance, version fromAccountwhereId="1"; Query Result: ID=1, balance=1100, version=2


An ORM framework, such as Hibernate, JPA, or implementation, that uses the version number to determine the value returned after the update

MyBatis optimistic lock implementation to solve concurrency problems

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.