1. Introduction to data concurrency control
Data concurrency control is a system used to process multiple modifications to persistent business objects at the same time point. When multiple users modify the state of the business object and try to persistently store it to the database concurrently, a mechanism is required to ensure that one user does not have a negative impact on the transaction state of another concurrent user.
There are two forms of concurrency control: optimistic and pessimistic. Optimistic Concurrency Control assumes that when multiple users modify the Business Object status at the same time, it will not cause any problems, also known as the last change wins ). This is a reasonable action for some systems. However, if the state of the business object needs to be consistent with the State retrieved from the database, pessimistic concurrency control is required.
Pessimistic concurrency control can have multiple styles. You can lock the data table After retrieving the records, or save copies of the original content of the business object, before updating, compare the copy with the version in the data storage. Make sure that the record is not modified during this transaction.
2. Example of data concurrency control
There are two common methods to implement data concurrency control: database implementation and code control implementation. Pessimistic concurrency control can be added to the database lock mechanism in terms of database implementation. In terms of code control implementation, a save version number field can be added for comparison between versions. Use the version number to check whether the business entity is modified after it is retrieved from the database. When updating, compare the version number of the business entity with the version number in the database before submitting the modification. This ensures that the business entity is not modified after it is retrieved.