3.6 - data con currency

Upload: eric-law

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 3.6 - Data Con Currency

    1/25

    2012 IBM Corporation

    Information Management

    Information Management Technology EcosystemIBM Canada Lab

    Winter / Spring 2012

    Data Concurrency

  • 8/2/2019 3.6 - Data Con Currency

    2/25

    2 2012 IBM Corporation

    Information Management

    Agenda

    Database Transactions

    Concurrency

    Concurrency Issues

    Concurrency Control

    Isolation LevelsLockingSpecifying Isolation Levels

  • 8/2/2019 3.6 - Data Con Currency

    3/25

    3 2012 IBM Corporation

    Information Management

    Database Transactions

    Transactionsequence of one or more SQL operations, grouped together

    as a single unitalso known as a unit of work

    Committed Data

    using the COMMIT statement commits any changes madeduring the transaction to the database

    Uncommitted Datachanges during the transaction before the COMMIT

    statement is executed

  • 8/2/2019 3.6 - Data Con Currency

    4/25

    4 2012 IBM Corporation

    Information Management

    Concurrency

    ConcurrencySharing of resources by multiple interactive users or

    application programs at thesame time

    Having multiple interactive users can lead to:Lost Update

    Uncommitted ReadNon-repeatable ReadPhantom Read

    Need to be able to control the degree of concurrency:With proper amount ofdata stability

    Without loss ofperformance

  • 8/2/2019 3.6 - Data Con Currency

    5/25

    5 2012 IBM Corporation

    Information Management

    Concurrency Issues

    Lost UpdateOccurs when two transactions read and then attempt to

    update the same data, the second update will overwrite thefirst update before it is committed

    1) Two applications, A and B, both read the same row andcalculate new values for one of the columns based on thedata that these applications read

    2) A updates the row

    3) Then B also updates the row

    4) A's update lost

  • 8/2/2019 3.6 - Data Con Currency

    6/25

    6 2012 IBM Corporation

    Information Management

    Concurrency Issues

    Uncommitted ReadOccurs when uncommitted data is read during a transactionAlso known as a Dirty Read

    1) Application A updates a value2) Application B reads that value before it is committed

    3) A backs out of that update3) A backs out of that update

    4) Calculations performed by B are based on theuncommitted data

  • 8/2/2019 3.6 - Data Con Currency

    7/257 2012 IBM Corporation

    Information Management

    Concurrency Issues

    Non-repeatable Read

    Occurs when a transaction reads the same row of data twiceand returns different data values with each read

    1) Application A reads a row before processing otherrequests

    2) Application B modifies or deletes the row and commitsthe change

    3) A attempts to read the original row again

    4) A sees the modified row or discovers that the originalrow has been deleted

  • 8/2/2019 3.6 - Data Con Currency

    8/258 2012 IBM Corporation

    Information Management

    Concurrency Issues

    Phantom Read

    Occurs when a search based on some criterion returnsadditional rows afterconsecutive searches during atransaction

    1) Application A executes a query that reads a set ofrows based on some search criterion

    2) Application B inserts new data that would satisfyapplication A's query

    3) Application A executes its query again, within thesame unit of work, and some additional phantom valuesare returned

  • 8/2/2019 3.6 - Data Con Currency

    9/259 2012 IBM Corporation

    Information Management

    Concurrency Control

    Isolation Levels

    determine how data is locked or isolated from otherconcurrently executing processes while the data is beingaccessed

    are in effect while the transaction is in progress

    There are four levels of isolation in DB2:Repeatable readRead stabilityCursor stability

    Currently Committed (introduced in DB2 9.7)

    Uncommitted read

  • 8/2/2019 3.6 - Data Con Currency

    10/2510 2012 IBM Corporation

    Information Management

    DB2 Isolation Levels

    Application Type High data stability

    required

    High data stability not

    required

    Read-write transactions Read Stability (RS) Cursor Stability (CS)

    Read-only transactions Repeatable Read (RR) orRead Stability (RS)

    Uncommited Read (UR)

    Isolation Level Dirty Read Non-repeatableRead

    PhantomRead

    Repeatable Read (RR) - - -

    Read Stability (RS) - - Possible

    Cursor Stability (CS) - Possible Possible

    Uncommitted read (UR) Possible Possible Possible

    DEFAULT

    prior to 9.7

  • 8/2/2019 3.6 - Data Con Currency

    11/25

    11 2012 IBM Corporation

    Information Management

    Locking in DB2 Isolation levels are enforced by locks

    Locks limit or prevent data access by concurrent users or applications Before read/write data, transactions need to acquire the lock on the data

    Locking Attributes

    resource being locked is called object

    objects which can be explicitly locked are databases, tables and tablespaces

    objects which can be implicitly locked are rows, index keys, and tables implicit locks are acquired by DB2 according to isolation level and

    processing situations

    object being locked represents granularity of lock

    length of time a lock is held is called duration and is affected by isolationlevel

    Database Configuration Parameters (

    LOCKLIST: amount of storage allocated to the lock list

    MAXLOCKS: percentage of the lock list held by an application that must befilled before the database manager performs lock escalation

    Both can be automatically managed by DB2's Self-Tuning Memory Manager.

  • 8/2/2019 3.6 - Data Con Currency

    12/25

    12 2012 IBM Corporation

    Information Management

    Types of Locks DB2 for LUW

    Locks are acquired for all operations to control how otherapplications access the same resource.

    Examples of Types of Locks in DB2

    Share (S)

    Owner and concurrent transactions are limited to read only

    Update (U)

    Owner can read/write, but concurrent transactions arelimited to read-only operations

    Exclusive (X)

    Owner can read/write. Concurrent transactions cannotread/write. UR application can still read the data.

  • 8/2/2019 3.6 - Data Con Currency

    13/25

    13 2012 IBM Corporation

    Information Management

    Deadlock

    Deadlock Detector

    discovers deadlock cycles randomly selects one of the transactions involved to roll

    back and terminate

    transaction chosen is then sent an SQL error code, andevery lock it had acquired is released

    The frequency with which the deadlock detector getsinitiated can be controlled by setting a value fordlchktime a database configuration parameter

  • 8/2/2019 3.6 - Data Con Currency

    14/25

    14 2012 IBM Corporation

    Information Management

    Repeatable Read

    Highest level of isolation

    No dirty reads, non-repeatable reads or phantom reads

    Locks the entire table or view being scanned for a queryProvides minimum concurrency

    When to use Repeatable Read:

    Changes to the result set are unacceptableData stability is more important than performance

    000 ABC DEF DEF DEF YYY QWERT ZZ001 ABC DEF DEF DEF YYY QWERT ZZ

    002 ABC DEF DEF DEF YYY QWERT ZZ

    003 ABC DEF DEF DEF YYY QWERT ZZ

    004 ABC DEF DEF DEF YYY QWERT ZZ

    005 ABC DEF DEF DEF YYY QWERT ZZ

    006 ABC DEF DEF DEF YYY QWERT ZZ

    SELECT *

    FROM t1

    WHERE ID > 2

  • 8/2/2019 3.6 - Data Con Currency

    15/25

    15 2012 IBM Corporation

    Information Management

    Read Stability

    Similar to Repeatable Read but not as strict

    No dirty reads or non-repeatable reads; Phantom reads can occur

    Locks only the retrieved or modified rows in a table or view

    When to use Read Stability:

    Application needs to operate in a concurrent environment Qualifying rows must remain stable for the duration of the UoW

    If the same query is issued more than once during a unit of work, thesame result set should not be required

    000 ABC DEF DEF DEF YYY QWERT ZZ001 ABC DEF DEF DEF YYY QWERT ZZ

    002 ABC DEF DEF DEF YYY QWERT ZZ

    003 ABC DEF DEF DEF YYY QWERT ZZ

    004 ABC DEF DEF DEF YYY QWERT ZZ

    005 ABC DEF DEF DEF YYY QWERT ZZ

    006 ABC DEF DEF DEF YYY QWERT ZZ

    SELECT *

    FROM t1

    WHERE ID > 2

  • 8/2/2019 3.6 - Data Con Currency

    16/25

    16 2012 IBM Corporation

    Information Management

    Cursor Stability

    Default isolation level

    No dirty readsNon-repeatable reads and phantom reads can occur

    Locks only the row currently referenced by the cursor

    When to use Cursor Stability:

    Want maximum concurrency while seeing only committeddata

    000 ABC DEF DEF DEF YYY QWERT ZZ001 ABC DEF DEF DEF YYY QWERT ZZ

    002 ABC DEF DEF DEF YYY QWERT ZZ

    003 ABC DEF DEF DEF YYY QWERT ZZ

    004 ABC DEF DEF DEF YYY QWERT ZZ

    005 ABC DEF DEF DEF YYY QWERT ZZ

    006 ABC DEF DEF DEF YYY QWERT ZZ

    SELECT *

    FROM t1

    WHERE ID > 2

  • 8/2/2019 3.6 - Data Con Currency

    17/25

    17 2012 IBM Corporation

    Information Management

    Uncommitted Read

    Lowest level of isolation

    Dirty reads, non-repeatable reads and phantom reads canoccur

    Locks only rows being modified in a transaction involvingDROP or ALTER TABLE

    Provides maximum concurrency When to use Uncommitted Read:

    Querying read-onlytablesUsing only SELECT statementsRetrieving uncommitted data is acceptable

  • 8/2/2019 3.6 - Data Con Currency

    18/25

    18 2012 IBM Corporation

    Information Management

    Isolation Levels

    Summary

    Application Type High data stability

    required

    High data stability not

    required

    Read-write transactions Read Stability (RS) Cursor Stability (CS)

    Read-only transactions Repeatable Read (RR) orRead Stability (RS)

    Uncommited Read (UR)

    Isolation Level Dirty Read Non-repeatableRead

    PhantomRead

    Repeatable Read (RR) - - -

    Read Stability (RS) - - Possible

    Cursor Stability (CS) - Possible Possible

    Uncommitted read (UR) Possible Possible PossibleDEFAULT

  • 8/2/2019 3.6 - Data Con Currency

    19/25

    19 2012 IBM Corporation

    Information Management

    Currently Committed

    Currently Committed is a variation on Cursor Stability

    Avoids timeouts and deadlocksLog based:

    No management overhead

    Situation Result

    Reader blocks Reader No

    Reader blocks Writer Maybe

    Writer blocks Reader Yes

    Writer blocks Writer Yes

    Situation Result

    Reader blocks Reader No

    Reader blocks Writer No

    Writer blocks Reader No

    Writer blocks Writer Yes

    Cursor Stability Currently Committed

  • 8/2/2019 3.6 - Data Con Currency

    20/25

    20 2012 IBM Corporation

    Information Management

    Up to DB2 9.5

    Cursor Stabilityis the default isolation level

    Now in DB2 9.7Currently Committed is the default forNEW databasesCurrently Committed is disabled for upgraded databases,

    i.e., Cursor Stability semantics are used Applications that depend on the old behavior (writers

    blocking readers) will need to update their logic or disablethe Currently Committed semantics

    Currently Committed

  • 8/2/2019 3.6 - Data Con Currency

    21/25

    21 2012 IBM Corporation

    Information Management

    Currently Committed How to use it?

    cur_commit DB config parameter

    ON: default for new DBs created in DB2 9.7 CC semanticsin place

    DISABLED: default value for existing DBs old CSsemantics in place

    PRECOMPILE/BIND

    CONCURRENTACCESSRESOLUTION: Specifies theconcurrent access resolution to use for statements in thepackage.

    USE CURRENTLY COMMITTED

    WAIT FOR OUTCOME

  • 8/2/2019 3.6 - Data Con Currency

    22/25

    22 2012 IBM Corporation

    Information Management

    Specifying Isolation Levels

    Precompile / Bind

    ISOLATION option of PREP or BIND commandCan determine isolation level of a package by executing the

    following query

    Statement LevelUse the WITH {RR, RS, CS, UR} clause

    The WITH UR option applies only to read-only operations

    ensure that a result table is read-only by specifying FORREAD ONLY in the SQL statement

    Overrides the isolation level specified for the package

    SELECT * FROM tb1 WITH RR

    SELECT ISOLATION FROM syscat.packages

    WHERE pkgname = 'pkgname'

    AND pkgschema = 'pkgschema'

  • 8/2/2019 3.6 - Data Con Currency

    23/25

    23 2012 IBM Corporation

    Information Management

    Specifying Isolation Levels

    Dynamic SQL within the current session

    SET CURRENT ISOLATIONFor all subsequent dynamic SQL statements within the same

    session

    JDBC or SQLJ at run timeSQLJ profile customizer (db2sqljcustomize command)

    CLI or ODBC at run timeCHANGE ISOLATION LEVEL command changes the isolation

    level for the current connection

    CHANGE ISOLATION LEVELTO RR

  • 8/2/2019 3.6 - Data Con Currency

    24/25

    24 2012 IBM Corporation

    Information Management

    Summary

    Concurrency control in DB2 is based on locking

    DB2 offers 4 isolation levels: Repeatable Read Read Stability Cursor Stability (default)

    Currently Committed Uncommitted Read

    In DB2 9.7, with Currently Committed semantics, readersalways received the last committed version of the data anddon't wait for writers.

    Isolation levels can be modified during the pre-compilationphase or at runtime. It can be changed for the remaining of asession, or for a specific SQL statement only.

  • 8/2/2019 3.6 - Data Con Currency

    25/25

    2012 IBM Corporation

    Information Management

    Questions?

    E-mail: [email protected]: DB2 Bootcamp

    Information Management Technology EcosystemIBM Canada Lab

    Winter / Spring 2012