Locking and Blocking
Locking and Blocking
This anomaly can occur if shared locks are not acquired for
reads, since there is no lock to conflict with the exclusive lock
taken out by Transaction1.
Transactional Anomalies
Read Uncommitted
• The least restrictive isolation level.
• Acquiring locks for write operations but not
acquiring any locks for read operations.
• Read operations do not block other readers or
writers.
=> all transactional anomalies are possible.
Pessimistic Isolation Levels
Read Committed
• Default isolation level
• Acquiring shared locks for read operations as
well as locks for write operations.
• Shared locks are only held during the read phase
of a specific row, and the lock is released as soon
as the record has been read.
=> protection against dirty reads, but nonrepeatable
reads and phantom reads are still possible.
Pessimistic Isolation Levels
Repeatable Read
Acquires shared locks on all rows that it touches
and then it holds these locks until the end of the
transaction.
=> dirty reads and nonrepeatable reads are not
possible,although phantom reads can still occur.
Pessimistic Isolation Levels
Serializable
• most restrictive isolation level
• not only acquiring locks for write operations but
also by acquiring key-range locks for read
operations and then holding them for the duration
of the transaction.
=> no transactional anomalies are possible,
including phantom reads.
Optimistic Isolation Levels
• Not acquiring any locks for either read or write
operations.
• Use a technique called row versioning.
• Maintaining a new copy of a row in TempDB for
uncommitted transactions every time the row is updated.
Dramatically reduce contention on highly concurrent
systems.
=>The trade-off is that you need to scale TempDB
appropriately, in terms of both size and throughput capacity
Need to turn on optimistic isolation levels at the database
level.
Optimistic Isolation Levels
Snapshot isolation
• Uses optimistic concurrency for both read and write
operations.
• Assigning each transaction a transaction sequence
number at the point the transaction begins.
• Reads a row within the transaction and retrieves the
row version from tempdb whose sequence number is
closest to, and lower than, the transaction sequence
number.
=> dirty reads, nonrepeatable reads, and phantom reads
are not possible.
Optimistic Isolation Levels
Isolation Levels
Transaction with In-Memory OLTP
Read Committed isolation level is supported against
memory-optimized tables only for autocommit
transactions.
It is also not possible to use Read Committed in the
ATOMIC block of a natively compiled stored procedure.
Snapshot
Snapshot isolation is only supported against memory-
optimized tables when you use interpreted SQL if it is
specified as a query hint as opposed to at the transaction
level.
It is fully supported in the ATOMIC block of natively
compiled stored procedures.
Transaction with In-Memory OLTP
Repeatable Read
The guarantee provided by REPEATABLE READ
isolation is that, at commit time, no concurrent
transaction has updated any of the rows read by
this transaction.
Serializable
it guarantees that no rows have been inserted
within the range of rows being accessed by queries
within the transaction.
Transaction with In-Memory OLTP
Cross-Container Transactions
when a transaction accesses both memory-
optimized tables and disk-based tables, you may
need to specify a combination of isolation levels
and query hints.
Retry Logic
Whether you are using interpreted SQL or a natively compiled stored
procedure, always ensure that you use retry logic when you are
running transactions against memory-optimized tables.
Because of the optimistic concurrency mode, conflict detection
mechanism rolls transactions back, as opposed to managing
concurrency with locking
Observing Transactions, Locks, and Deadlocks