Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are locking mechanisms (Pessimistic/Optimistic) related to database transaction isolation levels?

Tags:

I am writing a web application where two different users can update a list of things, to do list, for example. I have come to realize that, optimistic locking mechanism works best since I don't expect high contention.

I was looking at transaction isolation levels and now I am a little confused. Looks like different transaction isolation levels also solve similar problems.

How are these two different concepts related to each other? If possible, with a simple example.

like image 888
Abhishek Shukla Ravishankara Avatar asked Mar 25 '14 21:03

Abhishek Shukla Ravishankara


People also ask

What is pessimistic locking and optimistic locking?

There are two models for locking data in a database: Optimistic locking , where a record is locked only when changes are committed to the database. Pessimistic locking , where a record is locked while it is edited.

What is pessimistic locking in database?

Pessimistic Locking: When a user accesses an object to update it, the database locks the object until the update is completed. No other user can read or update the object until the first user releases the lock. The database offers this locking type. No Locking: The application does not verify that data is current.

How optimistic locking is implemented in database?

Optimistic locking is a technique for SQL database applications that does not hold row locks between selecting and updating or deleting a row. The application is written to optimistically assume that unlocked rows are unlikely to change before the update or delete operation.

What is a difference between optimistic concurrency control and pessimistic concurrency control also known as optimistic and pessimistic locking )?

Optimistic concurrency control is based on the idea of conflicts and transaction restart while pessimistic concurrency control uses locking as the basic serialization mechanism. Analytic and simulation models of both mechanisms were developed in order to compare them as far as transaction response time is concerned.


1 Answers

Both of these things are related to data consistency and concurrent access, but they are two different mechanisms.

Locking prevents concurrent access to some object. For example when you attempt to update a todo list item, with pessimistic locking database places a row lock on the record until you either commit or rollback the transaction, so that no other transaction is allowed to update the same record. Optimistic locking is application-side check whether the timestamp/version of a record has changed between fetching and attempting to update it. This is regardless of transaction isolation level.

Transaction isolation is about read consistency.

  • Read uncommitted level allows session to see other session's uncommitted changes
  • Read committed level allows session to see other session's committed changes only
  • Serializable level allows session to see only changes committed before the transaction began

Take a look at below example, I indicated the query results that differ between transaction isolation levels.

SESSION 1                                  SESSION 2 --------------------------------           -------------------------------------- SELECT count(*) FROM test; => 10                                            INSERT INTO test VALUES ('x');  SELECT count(*) FROM test; => 10 with read committed/serializable => 11 with read uncommited (dirty read)                                            COMMIT;  SELECT count(*) FROM test; => 10 with serializable => 11 with read uncommitted/read committed 

There are four ANSI specified transaction isolation levels (one not mentioned in the example above is "repeatable read"), all of them except serializable are subjects to some anomalies. Note it has nothing to do with locking.

You can take a look at Oracle documentation on this here, the concepts are quite universal.

Finally, your approach to use optimistic locking seems sensible for a web application. Most probably you fetch a list item and update it in two different HTTP requests. It is impossible (or unwise at least) to keep transaction open with explicit lock on the record after the fetch (how do you know whether the second request will arrive at all?) Optimistic locking handles this gracefully.

like image 147
Kombajn zbożowy Avatar answered Sep 29 '22 10:09

Kombajn zbożowy