DB2 Data Con Currency

Embed Size (px)

Citation preview

  • 8/6/2019 DB2 Data Con Currency

    1/9

    1

    IBM DB2 9

    2008 IBM Corporation

    Section -6) Data ConcurrencyData Concurrency

    IBM DB2 9

    2

    Section 6Section 6 - - Data Concurrency (11%)Data Concurrency (11%)

    Ability to identify factors that influence lockingAbility to identify factors that influence lockingAbility to list objects on which locks can beAbility to list objects on which locks can beobtainedobtainedAbility to identify characteristics of DB2 locksAbility to identify characteristics of DB2 locksGiven a situation, ability to identify the isolationGiven a situation, ability to identify the isolationlevel that should be usedlevel that should be used

    IBM DB2 9

    3

    Database Transactions

    A transaction (also known as a unit of work ) is arecoverable sequence of one or more SQL operations,grouped together as a single unit, usually within anapplication process.Role of COMMIT, Auto-Commit and ROLLBACKACID = Atomicity, Consistency, Isolation and DurabilityIn single-user environments , each transaction runsserially and doesn't interfere to other transactions.In multi-user environments , transactions can (and oftendo) run simultaneously. As a result, each transaction hasthe potential to interfere with other active transactions.

    IBM DB2 9

    4

    Simple workload consisting of three transactions

    CONNECT TO my_db

    CREATE TABLE department (dept_id INTEGER NOT NULL,dept_name VARCHAR(20))

    INSERT INTO department VALUES(100, 'PAYROLL')

    INSERT INTO department VALUES(200, 'ACCOUNTING')

    COMMIT;

    INSERT INTO department VALUES(300, 'SALES')

    ROLLBACK;

    INSERT INTO department VALUES(500, 'MARKETING')

    COMMIT;

  • 8/6/2019 DB2 Data Con Currency

    2/9

    2

    IBM DB2 9

    5

    IBM DB2 9

    6

    Possible Issues In Multiple-user Scenario

    Lost Update - This occurs when two transactions readand then attempt to update the same data, and one of theupdates is lost.Uncommitted Read - This occurs when a transactionreads data that has not yet been committed.Non-repeatable Read - This occurs when a transactionreads the same row of data twice, but gets different datavalues each time.Phantom Read - This occurs when a row of data thatmatches search criteria is not seen initially, but then seenin a later read operation.

    IBM DB2 9

    7

    Isolation levels to enforce concurrencyMaintaining database consistency and data integrity, whileallowing more than one application to access the same dataat the same time, is known as concurrency .DB2 attempts to enforce concurrency is through the use of

    given four isolation levels , which determine how dataused in one transaction is locked or isolated from othertransactions while the first transaction works with it.

    - Repeatable read- Read stability- Cursor stability- Uncommitted read

    IBM DB2 9

    8

    Uncommitted Read (UR)

    Least restrictive isolation level available.Allows an application to access uncommitted changes of otherapplications.When this isolation level is used, rows retrieved by a

    transaction are only locked if the transaction modifies dataassociated with one or more rows retrieved or if anothertransaction drop or alter the table the rows were retrieved from.When this isolation level is used, dirty reads, non-repeatablereads, and phantoms can occur.Use it if you're executing queries on read-onlytables/views/databases or if it doesn't matter whether a queryreturns uncommitted data values.

  • 8/6/2019 DB2 Data Con Currency

    3/9

    3

    IBM DB2 9

    9

    IBM DB2 9

    10

    Cursor Stability (CS)

    Default isolation level.This isolation level only locks the row that is currentlyreferenced by a cursor that was declared and openedby the owning transaction. The lock remains in effectuntil next row is fetched or transaction is terminated.When this isolation level is used, lost updates and dirtyreads cannot occur; non-repeatable reads and

    phantoms can and may be seen.Use the Cursor Stability isolation level when you wantmaximum concurrency between applications, yet youdon't want queries to see uncommitted data.

    IBM DB2 9

    11

    IBM DB2 9

    12

    Read Stability (RS)

    When this isolation level is used, only rows thatare actually retrieved or modified by the owningtransaction are locked.When this isolation level is used, lost updates,dirty reads, and non-repeatable reads cannotoccur; phantoms, however, can and may be seen.Use the Read Stability isolation level when youwant some level of concurrency betweenapplications, yet you also want qualified rows toremain stable for the duration of an individualtransaction.

  • 8/6/2019 DB2 Data Con Currency

    4/94

    IBM DB2 9

    13

    IBM DB2 9

    14

    Repeatable Read (RR)

    Most restrictive isolation level availableIf an entire table or view is scanned in response to a query,the entire table or all table rows referenced by the view arelocked. This greatly reduces concurrency, especially whenlarge tables are used.Lost updates, dirty reads, non-repeatable reads, andphantoms cannot occur.Use the Repeatable Read isolation level if you're executinglarge queries and you don't want concurrent transactions tohave the ability to make changes that could cause thequery to return different results if run more than once.

    IBM DB2 9

    15

    IBM DB2 9

    16

    Specifying isolation levels with queries

    WITH clause (WITH [RR | RS | CS | UR]) that can be appended to aSELECT statement to set a specific query's isolation level toRepeatable Read (RR), Read Stability (RS), Cursor Stability (CS), orUncommitted Read (UR). For Example:

    SELECT * FROM employee WHERE empid = '001' WITH RR

  • 8/6/2019 DB2 Data Con Currency

    5/95

    IBM DB2 9

    17

    Locking

    A lock is a mechanismthat is used to associatea data resource with asingle transaction, forthe sole purpose ofcontrolling how othertransactions interactwith that resource while

    it is associated with thetransaction that has itlocked.

    IBM DB2 9

    18

    Lock Attributes

    resource being locked is called object .

    objects which can be explicitly locked are databases,tables and table spaces.

    objects which can be implicitely locked are rows,index keys, tables. Implicit locks are acquired by DB2according to isolation level and processing situations.

    object being locked represents granularity of lock.length of time a lock is held is called duration and isaffected by isolation level.

    IBM DB2 9

    19

    How Locks Are Acquired

    ALTER TABLE [ TableName ] LOCKSIZE [ROW | TABLE]LOCKSIZE ROW (Default) will acquire row-level locks for everytransaction that accesses this table. LOCKSIZE TABLE willattempt to acquire table-level locks for every transaction thataccesses this table.LOCK TABLE [TableName] IN [SHARE | EXCLUSIVE] MODEA table-level Share (S) lock is acquired on behalf of therequesting transaction, and other concurrent transactions areallowed to read, but not change, data stored in the locked table.When table-level Exclusive (X) lock is acquired, and otherconcurrent transactions can neither access nor modify datastored in the locked table.

    IBM DB2 9

    20

  • 8/6/2019 DB2 Data Con Currency

    6/96

    IBM DB2 9

    21

    IBM DB2 9

    22

    IBM DB2 9

    23

    Deadlock

    If the deadlock detectordiscovers a deadlockcycle, it randomly selects

    one of the transactionsinvolved to roll back andterminate; thetransaction chosen isthen sent an SQL errorcode, and every lock ithad acquired is released.

    IBM DB2 9

    24

  • 8/6/2019 DB2 Data Con Currency

    7/97

    IBM DB2 9

    25

    1. Application A holds an Exclusive lock on table TAB1 andneeds to acquire an Exclusive lock on table TAB2. ApplicationB holds an Exclusive lock on table TAB2 and needs to acquirean Exclusive lock on table TAB1. If lock timeout is set to -1 andboth applications are using the Read Stability isolation level,which of the following will occur?

    A. Applications A and B will cause a deadlock situationB. Application B will read the copy of table TAB1 that wasloaded into memory when Application A first read it

    C. Application B will read the data in table TAB1 and seeuncommitted changes made by Application AD. Application B will be placed in a lock-wait state untilApplication A releases its lock

    IBM DB2 9

    26

    2. Two applications have created a deadlock cycle in thelocking subsystem. If lock timeout is set to 30 and bothapplications were started at the same time, what action will thedeadlock detector take when it "wakes up" and discovers thedeadlock?A. It will randomly pick an application and rollback its currenttransactionB. It will rollback the current transactions of both applicationsC. It will wait 30 seconds, then rollback the currenttransactions of both applications if the deadlock has not beenresolvedD. It will go back to sleep for 30 seconds, then if the deadlockstill exists, it will randomly pick an application and rollback itscurrent transaction

    IBM DB2 9

    27

    3. Application A is running under the RepeatableRead isolation level and holds an Update lock ontable TAB1. Application B wants to query table TAB1and cannot wait for Application A to release its lock.Which isolation level should Application B run under

    to achieve this objective?

    A. Repeatable ReadB. Read StabilityC. Cursor StabilityD. Uncommitted Read

    IBM DB2 9

    28

    4. Application A holds a lock on a row in table TAB1. Iflock timeout is set to 20, what will happen whenApplication B attempts to acquire a compatible lockon the same row?A. Application B will acquire the lock it needsB. Application A will be rolled back if it still holds itslock after 20 seconds have elapsedC. Application B will be rolled back if Application Astill holds its lock after 20 seconds have elapsedD. Both applications will be rolled back if ApplicationA still holds its lock after 20 seconds have elapsed

  • 8/6/2019 DB2 Data Con Currency

    8/98

    IBM DB2 9

    29

    5. To which of the following resourcescan a lock NOT be applied?A. TablespacesB. Buffer poolsC. Tables

    D. Rows

    IBM DB2 9

    30

    6. Which of the following modes, when used

    with the LOCK TABLE statement, will causethe DB2 Database Manager to acquire a table-level lock that prevents other concurrenttransactions from accessing data stored in thetable while the owning transaction is active?A. SHARE MODE

    B. ISOLATED MODEC. EXCLUSIVE MODED. RESTRICT MODE

    IBM DB2 9

    31

    7. An application has acquired a Share lock on a rowin a table and now wishes to update the row. Whichof the following statements is true?A. The application must release the row-level Sharelock it holds and acquire an Update lock on the row

    B. The application must release the row-level Sharelock it holds and acquire an Update lock on the tableC. The row-level Share lock will automatically beconverted to a row-level Update lockD. The row-level Share lock will automatically beescalated to a table-level Update lock

    IBM DB2 9

    32

    8. Application A wants to read a subset of rowsfrom table TAB1 multiple times. Which of thefollowing isolation levels should Application Ause to prevent other users from making

    modifications and additions to table TAB1 thatwill affect the subset of rows read?A. Repeatable ReadB. Read StabilityC. Cursor StabilityD. Uncommitted Read

  • 8/6/2019 DB2 Data Con Currency

    9/99

    IBM DB2 9

    33

    9. A transaction using the Read Stability isolation level

    scans the same table multiple times before it terminates.Which of the following can occur within this transaction'sprocessing?A. Uncommitted changes made by other transactions canbe seen from one scan to the next.B. Rows removed by other transactions that appeared inone scan will no longer appear in subsequent scans.C. Rows added by other transactions that did not appearin one scan can be seen in subsequent scans.D. Rows that have been updated can be changed byother transactions from one scan to the next.

    IBM DB2 9

    34

    10. Application A issues the following SQL statements within a singletransaction using the Uncommitted Read isolation level:SELECT * FROM department WHERE deptno = 'A00';

    UPDATE department SET mgrno = '000100' WHERE deptno = 'A00';

    As long as the transaction is not committed, which of the followingstatements is FALSE?A. Other applications not running under the Uncommitted Readisolation level are prohibited from reading the updated rowB. Application A is allowed to read data stored in another table, evenif an Exclusive lock is held on that tableC. Other applications running under the Uncommitted Read isolationlevel are allowed to read the updated rowD. Application A is not allowed to insert new rows into theDEPARTMENT table as long as the current transaction remainsactive

    IBM DB2 9

    35

    Japanese

    Hebrew

    ThankYouEnglish

    MerciFrench

    Russian

    DankeGerman

    GrazieItalian

    GraciasSpanish

    ObrigadoPortuguese

    Arabic

    Simplified Chinese

    Traditional Chinese

    Tamil

    Thai

    Korean