NHibernate Tour (7): Preliminary exploration of concurrency control in NHibernate

Source: Internet
Author: User

The contents of this section

What is concurrency control?

Pessimistic concurrency control (pessimistic concurrency)

Optimistic concurrency control (optimistic concurrency)

NHibernate Support optimistic concurrency control

Example analysis

Conclusion

What is concurrency control?

When many people try to modify the data in the database at the same time, a control system must be implemented so that changes made by one person do not adversely affect the changes made by others. This is called concurrency control.

A simple understanding is that 2 or more users edit the same data at the same time. The users here may be: The actual user, different services, different code snippets (using multithreading), and what might happen in disconnected and connected situations.

Concurrent control theory is divided into two categories according to the method of establishing concurrent control:

Pessimistic concurrency control (pessimistic concurrency)

A locking system that prevents users from modifying data in a way that affects other users. If a user performs an action that causes a lock to be applied, only the owner of the lock releases the lock, and other users can perform actions that conflict with the lock. This approach is called pessimistic concurrency control because it is used primarily in environments where data contention is intense, and in environments where the cost of protecting data with locks is lower than the cost of rolling back transactions when concurrent conflicts occur.

Simple understanding is usually done through the "exclusive lock" approach. Gets the lock to block access to data that is being used by another process. In other words, the reader and the writer are blocking each other, which can cause data synchronization conflicts.

Optimistic concurrency control (optimistic concurrency)

In optimistic concurrency control, data is not locked when the user reads the data. When a user updates the data, the system checks to see if the user has changed the data after reading the data. If another user updates the data, an error occurs. In general, the user who receives the error message rolls back the transaction and starts over again. This approach is called optimistic concurrency control because it is used primarily in environments where data contention is not large and the cost of occasionally rolling back transactions is lower than the cost of locking data when reading data.

(above from SQL Server2008 MSDN documentation)

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.