2.1 - Data Concurrency - Odp
2.1 - Data Concurrency - Odp
Data Concurrency
Information Management
Information Management
Agenda
Database Transactions
Concurrency
Concurrency Issues
Concurrency Control
Isolation Levels
Locking
Specifying Isolation Levels
Information Management
Database Transactions
Transaction
sequence of one or more SQL operations, grouped together
as a single unit that occurs in one atomic action
also known as a unit of work
Information Management
Database Transactions
Committed Data
data consistent with the database
changes can always become committed data manually using
a COMMIT statement
committed data can only be reversed/removed with new
SQL statements (within a new transaction)
accessible to all users and applications
Uncommitted Data
data inconsistent with the database
changes that occur during the transaction before a COMMIT
statement is executed
changes can be reversed with ROLLBACK
inaccessible to other users and applications unless
Uncommitted Read is used
2010 IBM Corporation
Information Management
Database Transactions
Connection to a
database defines
first initiation
CONNECT TO DB employees
INSERT INTO employee VALUES (100, 'JOHN')
INSERT INTO employee VALUES (200, 'MANDY')
COMMIT
empID
name
100
JOHN
200
MANDY
empID
name
100
JOHN
200
MANDY
empID
name
100
JACK
200
MANDY
2010 IBM Corporation
Information Management
Concurrency
Concurrency
Sharing of resources by multiple interactive users or
application programs at the same time
Information Management
Concurrency Issues
Lost Update
Occurs when two transactions read and then attempt to
update the same data, the second update will overwrite the
first update before it is committed
1) Two applications, A and B, both read the same row and
calculate new values for one of the columns based on the
data that these applications read
2) A updates the row
3) Then B also updates the row
4) A's update lost
Information Management
Concurrency Issues
Uncommitted Read
Occurs when uncommitted data is read during a transaction
Also known as a Dirty Read
Information Management
Concurrency Issues
Non-repeatable Read
Occurs when a transaction reads the same row of data twice
and returns different data values with each read
Information Management
Concurrency Issues
Phantom Read
Occurs when a search based on some criterion returns
additional rows after consecutive searches during a
transaction
1) Application A executes a query that reads a set of
rows based on some search criterion
2) Application B inserts new data that would satisfy
application A's query
3) Application A executes its query again, within the
same unit of work, and some additional phantom values
are returned
10
Information Management
Concurrency Control
Isolation Levels
determine how data is locked or isolated from other
concurrently executing processes while the data is being
accessed
are in effect while the transaction is in progress
Uncommitted read
11
Information Management
Locking
12
Locking Attributes
resource being locked is called object
objects which can be explicitly locked are databases, tables
and table spaces
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 isolation level
Information Management
Types of Locks
13
Share (S)
concurrent transactions are limited to read-only operations
Update (U)
concurrent transactions are limited to read-only operations
if the transactions have not declared that they might update
a row, the database manager assumes that transaction
currently looking at a row might update it
Exclusive (X)
concurrent transactions are prevented from accessing the
data in any way
does not apply to transactions with an isolation level of UR
Information Management
Deadlock
Deadlock
Occurs when 2 (or more) competing operations are waiting
for each other to free some resource, but neither does, thus
the operations will never finish. Eg:
14
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, and
every lock it had acquired is released
Information Management
Repeatable Read
15
Information Management
Read Stability
16
Information Management
Cursor Stability
17
Information Management
Currently Committed
Cursor Stability
18
Currently Committed
Situation
Result
Situation
Result
No
No
Maybe
No
Yes
No
Yes
Yes
Information Management
Currently Committed
19
Up to DB2 9.5
Cursor Stability is the default isolation level
Information Management
Transaction B
Waiting because is
reading uncommitted data
Waiting because is
reading uncommitted data
DEADLOCK!!
20
Information Management
Transaction B
No locking
Reads last committed version
of the data
No
Nodeadlocks,
deadlocks,no
notimeouts
timeoutsin
inthis
thisscenario!
scenario!
21
Information Management
22
Information Management
Uncommitted Read
23
Information Management
Isolation Levels
24
Summary
Isolation Level
Dirty Read
Non-repeatable
Read
Phantom
Read
Possible
Possible
Possible
Possible
Possible
Possible
DEFAULT
Application Type
Read-write transactions
Read-only transactions
Information Management
Precompile / Bind
ISOLATION option of PREP or BIND command
Can determine isolation level of a package by executing the
following query
Statement Level
Use 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 FOR
READ ONLY in the SQL statement
Overrides the isolation level specified for the package
Information Management
26
Questions?
Summer/Fall 2010
E-mail: [email protected]
Subject: DB2 Academic Workshop
Information Management