For troubleshooting mybatis concurrency, refer to hibernate and mybatishibernate.

Source: Internet
Author: User

For troubleshooting mybatis concurrency, refer to hibernate and mybatishibernate.

A typical example is that two bank operators operate on the same account at the same time. For example, IF operator A and operator B read an account with A balance of 1000 yuan at the same time, operator A adds 100 yuan to the account, and operator B deducts 50 yuan from the account at the same time. Operator A submits the account first, submit after B. The actual account balance is 1000-50 = 950 yuan, but it should be 1000 + 100-50 = 1050. This is a typical concurrency issue.

A simple solution: Add 1 to the version number field during the UPDATE process, and then UPDATE returns the number of rows in the UPDATE result, which is determined by the number of rows.


Update account set price = # price #, version = version + 1

Where id = # id # and version = # vsersion #


If the number returned by the update execution is 0, it indicates that a concurrent modification is generated. You need to obtain the latest data and then perform the update operation.


An ORM framework or implementation, such as Hibernate and JPA, uses the version number to determine the value returned after UPDATE. If the value is less than 1, an optimistic lock exception is thrown for concurrent modification.

 

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.