Why you need a lock (concurrency control).
In a multiuser environment, multiple users may update the same record at the same time, which creates a conflict. This is the famous concurrency problem.
Typical conflicts are:
L Lost Update: The update of one transaction overrides the update result of other transactions, that is, the so-called update is lost. For example: User A changes the value from 6 to 2, and User B changes the value from 2 to 6, then user a loses his update.
L Dirty read: Dirty reads occur when a transaction reads another record that completes half of the transaction. For example: The user a,b sees the value is 6, the user B changes the value 2, the user a reads the value still is 6.
In order to solve the problems caused by these concurrency. We need to introduce concurrency control mechanisms. concurrency control mechanism
pessimistic Lock: it is assumed that concurrent conflicts will occur, shielding all operations that may violate data integrity. [1]
optimistic Lock: Assuming no concurrent conflicts occur, check for data integrity violations only when committing operations. [1] The optimistic lock does not solve the problem of dirty reading. optimistic Lock Application
1. Use a self growing integer to represent the data version number. Update to check the consistency of the version number, such as the data version in the database is 6, update the submission version=6+1, use the version value (=7) and the database Version+1 (=7) as compared, if equal, you can update, if it is not possible other programs have updated the record, So it returns an error.
2. Use time stamp to achieve.
Note: For both of these ways, Hibernate is implemented by: before adding annotation to the field using the optimistic lock: @Version, Hibernate automatically validates the field when it is updated. Pessimistic lock Application
Need to use the lock mechanism of the database, such as SQL Server TABLOCKX (exclusive table Lock) When this option is selected, SQL Server locks up the entire table until the command or transaction ends. This prevents other processes from reading or modifying data in the table.
used in SQL Server
Begin Tran
Select top 1 @TrainNo =t_no
From Train_ticket with (UPDLOCK) where s_flag=0
Update Train_ticket
Set T_name=user,
T_time=getdate (),
S_flag=1
where t_no= @TrainNo
Commit
We used the WITH (UPDLOCK) option in the query, and we added an update lock to the record when we queried the record, indicating that we were about to update the record. Note that update locks and shared locks are not conflicting, that is, other users can also query the contents of this table, but the update lock and exclusive lock are conflicting. So the other update users will block. Conclusions
In the actual production environment, if the concurrency is not large and dirty reading is not allowed, you can use pessimistic locks to solve the concurrency problem, but if the system concurrency is very large, pessimistic locking can cause very large performance problems, so we have to choose optimistic locking method.
Source: