.NET: Online pessimistic locking, optimistic locking online, offline pessimistic locking, optimistic locking code sample offline

Pessimistic locking

Definition: When I modify data, do not allow others to modify the data.

Intent: Concurrent easily occur (pessimists often considered a bad thing will happen to them).

Scene: performance scores process must modify the same rows as the evaluation results table after each evaluation score, and often scores in the same time period, scoring process is suitable for use pessimistic locking.

Optimistic locking

Definition: anyone can modify the data, the time of filing and database comparison, if the data has changed in the meantime, there are two treatment ideas: 1, failure; 2, after the merger submitted.

Intent: Concurrency is not likely to occur (often optimists believe that a bad thing does not happen to them).

Scene: more personnel administrator can modify the labor contract, because such concurrent scenario rarely occurs, so here will be much better use of optimistic locking.

Offline

Definition: Also called long transactions; a business process needs many times and server communication.

Intent: business requirements.

Scene: Modify the approval process will continue the process of multiple requests, multiple requests must be treated as a long transaction.

Online

Definition: Also called short transactions; and only one business process server communication once.

Intent: Business Requirements

Scene: Most businesses fall into this scenario.

Online optimistic locking

[Test Method]
        [ExpectedException ( typeof (DbUpdateConcurrencyException))]
         public  void optimistic locking test ()
        {
            using (var context1 = new TestEntities())
            {
                . context1.Users.First () the Name = " Li NIO 1 " ;

                using (var context2 = new TestEntities())
                {
                    . context2.Users.First () the Name = " Li NIO 2 " ;
                    context2.SaveChanges();
                }

                context1.SaveChanges();
            }
        }

Pessimistic Offline Lock

The difficulty is to solve offline pessimistic lock acquisition and release timing, the common idea is to determine whether the lock to open Form, if not locked, they acquire the lock, release the lock when closed Form, release all locks when the session expires. Lock management interface as follows:

public  interface the I Pessimistic Offline Lock Management Interface
    {
        bool whether the lock (Guid identification documents);

        void acquire the lock (Guid user identifier, Guid identification documents);

        void release lock (Guid user identifier, Guid identification documents);

        void release lock (Guid user identifier);
    }

 

Guess you like

Origin www.cnblogs.com/bangguo/p/12586161.html