DB2 Introduction - 11 Con Currency
DB2 Introduction - 11 Con Currency
November 2004
Agenda
Transactions Concurrency Isolation Levels Locking Monitoring Locks
November 2004
November 2004
Concurrency
November 2004
Concurrency Transactions
A transaction (otherwise known as a unit of work) is a recoverable sequence of one or more SQL operations grouped together as a single unit, usually within an application process Transactions are terminated by executing either the COMMIT or the ROLLBACK statement A commit or rollback operation only affects changes that are made within the transaction that the commit or rollback operation ends As long as data changes remain uncommitted, other users and applications are usually unable to see them Once data changes are committed, they can no longer be removed by a rollback operation
November 2004
Concurrency Definition
In a multi-user database environment transactions may execute simultaneously, and each has the potential to interfere with any other transaction that is running Maintaining database consistency and data integrity, while allowing more than one application to access the same data at the same time, is known as concurrency When transactions are not isolated from each other in multi-user environments, four types or phenomena can occur
November 2004
Concurrency Phenomena
Lost Update Two transactions read and attempt to update the same data, and one of the updates is lost DB2 does NOT allow this phenomenon to occur Dirty Read A transaction reads data that has not yet been committed Nonrepeatable Read A transaction reads the same row of data twice, but gets different data values each time Phantom A row of data that matches search criteria is not seen initially, but then seen in a later read operation
Introduction To DB2 11. Concurrency November 2004 2003 IBM Corporation
November 2004
November 2004
November 2004
November 2004
November 2004
Locks
November 2004
November 2004
Transactions Commit
START TRANSACTION END TRANSACTION
TIME
Locks are acquired at the start of and throughout the life of the transaction When the COMMIT statement is executed all changes are made permanent Locks are released when the transaction is terminated (by the COMMIT statement
November 2004
Transactions Rollback
START TRANSACTION END TRANSACTION
TIME
Locks are acquired at the start of and throughout the life of the transaction When the ROLLBACK statement is executed all changes are backed out and the database is rolled back to the state it was in before the transaction began Locks are released when the transaction is terminated (by the ROLLBACK statement
November 2004
Lock Attributes
All locks have the following basic attributes: Object: Identifies the data resource that is being locked, such as tablespaces, tables, and rows Size: specifies the physical size of the portion of the data resource that is being locked Duration: Specifies the length of time for which a lock is held (usually controlled by a transaction's isolation level) Mode: Specifies the type of access allowed for the lock owner as well as the type of access permitted for concurrent users of the locked data resource This attribute is commonly referred to as the lock state
November 2004
IS (Intent Share)
Rows
* Blocks are used in Multi-Dimensional Clustering (MDC) Introduction To DB2 11. Concurrency November 2004 2003 IBM Corporation
IX (Intent Exclusive)
Tables, blocks
U (Update)
November 2004
X (Exclusive)
November 2004
Z (Super Exclusive)
Hierarchy Of Locks
November 2004
November 2004
Lock Compatibility
Whenever one transaction holds a lock on a data resource and a second transaction requests a lock on the same resource, the DB2 Database Manager examines the two lock states to determine whether or not they are compatible If the state of one lock placed on a data resource enables another lock to be placed on the same resource, the two locks (or states) are said to be compatible If the locks are compatible, the lock is granted to the second transaction If however, the locks are incompatible, the second transaction must wait until the first transaction releases its lock before it can gain access to the resource and continue processing
November 2004
November 2004
Lock Conversion
When a transaction attempts to access a data resource that it already holds a lock on, and the mode of access needed requires a more restrictive lock than the one already held, the state of the lock held is changed to the more restrictive state The operation of changing the state of a lock already held to a more restrictive state is known as lock conversion Lock conversion occurs because a transaction can hold only one lock on a data resource at a time Once a lock's state has been converted, the lock stays at the highest state obtained until the transaction holding the lock is terminated
November 2004
Lock Escalation
In order to prevent a specific database agent from exceeding the lock space limitations established, a process known as lock escalation is performed automatically whenever too many locks (of any type) have been acquired Lock escalation is the conversion of several individual row-level locks within the same table to a single table-level lock A lock is escalated when: The total number of locks held by an application reaches the maximum amount of lock list space available to the application (MAXLOCKS) The lock list space consumed by all applications is approaching the total lock list space (LOCKLIST)
Introduction To DB2 11. Concurrency November 2004 2003 IBM Corporation
November 2004
Deadlocks
A deadlock is created when one application is waiting for another application to release a lock on data. Each of the waiting applications is locking data needed by another application. Mutual waiting for the other application to release a lock on held data leads to a deadlock When a deadlock situation occurs, all transactions involved will wait indefinitely for a lock to be released, unless some outside agent takes action The DB2 deadlock detector checks at preset intervals (DLCHKTIME configuration parameter) whether or not a deadlock situation exists If the deadlock detector discovers a deadlock in the locking subsystem, it selects, terminates, and rolls back one of the transactions involved The victim receives an SQL 911 reason code 2
Introduction To DB2 11. Concurrency November 2004 2003 IBM Corporation
Lock Timeouts
Without some sort of lock timeout detection mechanism in place, a transaction might wait indefinitely for a lock to be released A lock timeout value can be specified in the database configuration file (LOCKTIMEOUT parameter) This value controls the amount of time any transaction will wait to obtain a requested lock If the desired lock is not acquired before the time interval specified elapses, the waiting application receives an SQL 911 (reason code 68) error and the transaction requesting the lock is rolled back
November 2004
Monitoring Locks
November 2004
Levels Of Monitoring
Levels of monitoring Snapshot monitoring at the database, application, transaction and lock levels Event monitoring at the database, connection, transaction and deadlock event levels Performance considerations what to monitor Lock escalations Deadlocks Lock waits and timeouts
November 2004
November 2004
November 2004