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