Under Java multithreading, get the incremented serial number

Source: Internet
Author: User

Scenario Description:

  1, our system can be simply summed up into the MVC architecture pattern

2, each front-end requests come over, will open the transaction in the C layer, after the final processing end, also in the C layer shutdown transaction (actually is in the C layer of the bottom of the unified transaction to open and commit);

Problem Description:

There is an interface method that gets the sequence number in the database, and then +1 is saved to the database.

The customer's alarm is: in the case of concurrency, the number repeats, that is, multiple threads take the same serial number at the same time, instead of waiting for the last thread to have a value after the sequence +1.

Problem Analysis:

Initial analysis, there are two problems:

1, Method lock does not work

2, transaction commit with serial number +1 is problematic

Solution:

1, method to increase the static lock

2, inside the interface method, initiates a child thread, which is used to open a transaction, obtain the serial number, and then +1 commits the database, commits the transaction, and the child thread executes the end. (After the T.start () of the child thread, add the T.join ())

Under Java multithreading, get the incremented serial number

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.