Home > database >  The optimistic locking the realization way of the version problem
The optimistic locking the realization way of the version problem

Time:09-24

Using MySQL default isolation level, repeatable read, use the version=1, 2, 3, 4... To achieve optimistic locking, when two threads A and B at the same time operating A database of one row, if is open transaction first, then is to query the data, such as the following A, B to thread the query results are as follows:


Table: person
A: id name version
Tom 1

B: id name version
Tom 1
When A thread to update data
Update the set name='jack', version=+ 1 where id=1 version and the version=1
Then A thread to commit the transaction,
And then thread B will update the name value, but at this point B threads in the query select * from person
The result must be
B: id name version
Tom 1

If B at this time to update the name value, then the thread for the version B value is how to obtain, through the query must not, because the query results as above, so is in Java code after each update version value through the method of the parameter passed to the thread B, namely we are through the Java code in a version control to obtain the value of the version rather than the corresponding version in the database query value?
  • Related