Transaction isolation is critical concept in database management systems. Isolation is the I in the acronym acid. Isolation setting fine tunes the balance between performance and isolation anamolies potentially caused by concurrent execution of transactions(for example a transaction reading uncommitted data of another transaction) . RDMBS support transaction isolation levels READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE.  Note that a serializable execution is defined to be an execution of the operations of concurrently executing/ interleaved SQL-transactions that produces the same effect as serial execution (one after the other) of those same SQL-transactions

Concurrency Control mechanisms

2 phase locking/pessimistic concurrency control and optimistic concurrency control are two techniques used by databases for concurrency control to handle various anamolies which can arise during concurrent execution of transactions. Avoiding conflicts (two-phase locking) requires locking to control access to shared resources. Detecting conflicts (e.g. Optimistic locking/Multi-Version Concurrency Control) provides better concurrency at the expense of relaxing serializability )

Isolation levels

DB isolation levels provide the user of a system the ability to trade off isolation guarantees for improved performance.Isolation levels described by the SQL:1992 standard are  READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, and SERIALIZABLE. The default isolation varies in different RDBMS .The default isolation level for InnoDB is REPEATABLE READ. The default isolation level for MySql InnoDB is REPEATABLE READ. Note that the way ANSI thinks about it, if you eliminate dirty reads, non-repeatable reads, and phantom reads, you’re serializable. This is wrong. It turns out if you eliminate these three anomalies, what you have is a new isolation level which is called Snapshot Isolation which is commonly used . (Infact oracle calls its implementation of snapshot isolation serializable ) While the most db will handle dirty reads, non-repeatable reads, and phantom reads as described in sql standard, handling of other anamolies like lost updates , write skew varies from database to database. A major problem is that the standard does not define, nor provide correctness constraints on one of the most popular reduced isolation levels used in practice: snapshot isolation (nor any of its many variants—PSI, NMSI, Read Atomic, etc). By failing to provide a definition of snapshot isolation, differences in concurrency vulnerabilities allowed by snapshot isolation have emerged across systems. For instance Snapshot isolation typically should stop lost updates (when using first update/first commiter wins) but this is not the case in innodb.

Handling isolation anamolies via 2 phase locking

Note that in different RDBMS there may be some variation in way 2 phase locking is implemented. 

In brief the anamolies defined by sql standard are 

Following anamolies are not covered by SQL standard and are handling by various databases is non standard.

 

Handling isolation anamolies via MVCC

Critical points to remember

FAQ

Question : In mysql is SELECT FOR UPDATE required in isolation level REPETABLE READ to solve the lost update problem?

 Yes. The SQL standard says nothing about lost updates because the standard was designed when 2PL (Two-Phase Locking) was the Concurrency Control mechanism. If you use 2PL, then the Repeatable Read isolation level will, indeed, prevent a Lost Update.( (As explained in this article, if two transactions have read data then reads acquire shared lock which is released only on transaction completion , hence if either of the transaction tries to write then it will have acquired exclusive lock which will fail)However, MVCC can provide Repeatable Reads via multiple versions of a tuple, but, in order to prevent Lost Updates, they also need the transaction scheduler to track tuple modifications for the records read by a certain transaction. Apparently, InnoDB does not work like that. Shouldn't MySQL MVCC prevent Lost Update using database-level Pessimistic Locking resulting in transaction rollback?Well, MVCC does not use any Pessimistic Locking in Repeatable Read. The only locks taken are the gap and next-key locks taken on the clustered index, but those don't prevent Lost Updates. MySQL uses Pessimistic Locking for Serializable only, which provides a 2PL Concurrency Control model, even when using the MVCC-based InnoDB storage engine. Hence  Serializable isolation level will solve the lost update problem.

In a nutshell lost update anamoly is not part of standards like other anamolies like dirty read, non repteable read , phantom read. 2PL (eg in ms sql)addressess this issue when read locks are acquired for reads (and held till end of transaction). MVCC without locks theoretically should resolve lost update but does not do so for example in MY SQL- Inno Db (2PC is only used for serializable).

Read more here https://forums.mysql.com/read.php?22,56420,57733.

Question : What is a consistent read?How does it work in InnoDB?

A read operation that uses snapshot information to present query results based on a point in time, regardless of changes performed by other transactions running at the same time. With REPEATABLE READ isolation level, the snapshot is based on the time when the first read operation is performed. With READ COMMITTED isolation level, the snapshot is reset to the time of each consistent read operation.Consistent read is the default mode in which InnoDB processes SELECT statements in READ COMMITTED and REPEATABLE READ isolation levels. Because a consistent read does not set any locks on the tables it accesses, other sessions are free to modify those tables while a consistent read is being performed on the table.

Question : Does snapshot isolation block phantom read anamoly?

The 2PL-based Serializable isolation prevents Phantom Reads through the use of predicate locking while MVCC (Multi-Version Concurrency Control) database engines address the Phantom Read anomaly by returning consistent snapshots.

Question : Does snapshot isolation block read skew anamoly?

-

Question : In inno db gap locks are only used in REPETABLE READ and SERIALIZABLE isolation levels.REPETABLE READ uses MVCC in innodb . this implies that innodb may be using some form of first updater wins, not first commiter wins. is this correct?

Question : Do inserts create table lock?

No, read more here https://dev.mysql.com/doc/refman/8.0/en/innodb-locks-set.html