of 31
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
1/31
Transaction controlTransaction controland isolation levelsand isolation levels
in Oraclein OracleEvgeniya Kotzeva
Vereo Technologies
www.vereo.bg
academy.devbg.org www.devbg.org
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
2/31
ContentsContents
TransactionTransaction controlcontrol
Data Concurrency and ConsistencyData Concurrency and Consistency
in a Multiuser Environmentin a Multiuser Environment
LockingLocking
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
3/31
Database TransactionDatabase Transaction
A database transaction consists of one ofA database transaction consists of one of
thethe following:following:
DML statements which constitute oneDML statements which constitute one
consistentconsistent change to the datachange to the data
One DDL statementOne DDL statement
One DCL statementOne DCL statement
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
4/31
Oracle Transaction TypesOracle Transaction Types
Consists of only one DCL
statementData control language
(DCL)
Consists of only one DDL
statementData definition
language
(DDL)
Consists of any number of DML
statements that the Oracle
server treats as a single entity
or a logical unit of work
Data manipulation
language (DML)
DescriptionType
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
5/31
Transaction boundariesTransaction boundaries
A transaction begins withA transaction begins withthe first executable SQLthe first executable SQL
statement.statement.
A transaction ends withA transaction ends withone of the followingone of the following
events:events:
A COMMIT orA COMMIT or
ROLLBACK statement isROLLBACK statement isissuedissued
A DDL or DCL statementA DDL or DCL statement
executes (automaticexecutes (automatic
commit)commit)
The user exitsThe user exits iiSQL*PlusSQL*Plus
The system crashesThe system crashes
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
6/31
Advantages of COMMITAdvantages of COMMITand ROLLBACKand ROLLBACK
With COMMIT and ROLLBACK statements, youWith COMMIT and ROLLBACK statements, you
can:can:
Ensure data consistencyEnsure data consistency
Preview data changes before making changesPreview data changes before making changes
permanentpermanent
Group logically related operationsGroup logically related operations
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
7/31
Controlling transactionControlling transaction
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
8/31
COMMIT transactionCOMMIT transaction
BeforeBefore COMMITCOMMIT
generated rollbackgenerated rollback
segment records in bufferssegment records in buffers
in the SGAin the SGA
generated redo log entriesgenerated redo log entries
in the redo log buffer of thein the redo log buffer of the
SGA.SGA.
The changes have beenThe changes have beenmade to the databasemade to the database
buffers of the SGA.buffers of the SGA.
AfterAfterCOMMITCOMMIT
The internal transactionThe internal transaction
table for the associatedtable for the associated
rollback segment recordsrollback segment records
updated with SCNupdated with SCN
LGWR writes SGA redoLGWR writes SGA redo
log entries to the onlinelog entries to the online
redo log fileredo log file
Oracle releases locksOracle releases locks
Oracle marks theOracle marks the
transaction complete.transaction complete.
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
9/31
ROLLBACK transactionROLLBACK transaction
ROLLBACKROLLBACK
Oracle undoes allOracle undoes all
transaction changes usingtransaction changes using
the undo tablespace orthe undo tablespace or
rollback segmentsrollback segments
Oracle releases all theOracle releases all the
transactions locks of datatransactions locks of data
The transaction endsThe transaction ends
ROLLBACK to SAVEPOINTROLLBACK to SAVEPOINT
Oracle rolls back only theOracle rolls back only the
statements run after thestatements run after the
savepoint.savepoint.
Oracle preserves theOracle preserves the
specified savepoint, but allspecified savepoint, but all
savepoints that weresavepoints that were
established after theestablished after the
specified one are lostspecified one are lost
Oracle releases all tableOracle releases all table
and row locks acquiredand row locks acquired
since that savepointsince that savepoint
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
10/31
State of the DataState of the DataBefore COMMIT or ROLLBACKBefore COMMIT or ROLLBACK
The previous state of the dataThe previous state of the datacan be recoveredcan be recovered..
The current userThe current usercan reviewcan reviewthe results of thethe results of the
DML operations by using the SELECT statement.DML operations by using the SELECT statement.
Other usersOther userscan not viewcan not viewthe results of the DMLthe results of the DML
statements by the current user.statements by the current user.
The affected rowsThe affected rowsare lockedare locked
Other usersOther userscannot changecannot changethe data within thethe data within the
affected rows.affected rows.
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
11/31
State of the Data after COMMITState of the Data after COMMIT
Data changes are madeData changes are madepermanentpermanentin thein thedatabase.database.
The previous state of the dataThe previous state of the data is permanently lostis permanently lost..
All usersAll users can viewcan viewthe results.the results.
Locks on the affected rowsLocks on the affected rows are releasedare released; those; those
rows are available for other users to manipulate.rows are available for other users to manipulate.
All savepointsAll savepoints are erasedare erased..
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
12/31
Distributed databaseDistributed database
Distributed transactionDistributed transaction is a transaction thatis a transaction thatincludes one or more statements that update dataincludes one or more statements that update data
on two or more distinct nodes of a distributedon two or more distinct nodes of a distributed
databasedatabase
AA two-phase committwo-phase commit mechanism guaranteesmechanism guarantees thethe
data consistent in all nodes.data consistent in all nodes.
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
13/31
Autonomous transactionAutonomous transactionss
Autonomous transactions are independentAutonomous transactions are independenttransactions that can be called from within anothertransactions that can be called from within another
transactiontransaction
An autonomous transaction lets you leave theAn autonomous transaction lets you leave thecontext of the calling transactioncontext of the calling transaction
You can call autonomous transactions from withinYou can call autonomous transactions from within
a PL/SQL block by using the pragmaa PL/SQL block by using the pragma
AUTONOMOUS_TRANSACTION.AUTONOMOUS_TRANSACTION.
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
14/31
Data Concurrency andData Concurrency andConsistencyConsistency
Data concurrencyData concurrency means that many users canmeans that many users can
access data at the same time.access data at the same time.
Data consistencyData consistency means that each user sees ameans that each user sees aconsistent view of the data, including visibleconsistent view of the data, including visible
changes made by the users own transactions andchanges made by the users own transactions and
transactions of other users.transactions of other users.
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
15/31
The isolation models preventsThe isolation models prevents
Dirty readsDirty reads
Nonrepeatable (fuzzy) readsNonrepeatable (fuzzy) reads
Phantom readsPhantom reads
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
16/31
Isolation levels (SQL92) controlsIsolation levels (SQL92) controls
PhantomReadNonrepeatableReadDirtyReadIsolationLevel
NNNSerializable
YNNRepeatable read
YYNRead committed
YYYRead
uncommitted
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
17/31
Oracle isolation levelsOracle isolation levels
The transaction sees only those
changes that were committed at the
time the transaction began and do not
allow any DML statement
Read-only
Serializable transactions see only thosechanges that were committed at the
time the transaction began, plus its own
changes
Serializable
Each query executed by a transactionsees only data that was committed
before the query began (Oracle default
isolation level)
Read committed
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
18/31
T ti d R dT ti d R d
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
19/31
Transactions and ReadTransactions and ReadConsistencyConsistency
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
20/31
Snapshot too oldSnapshot too old
When commit or rollback has been executed,When commit or rollback has been executed,the pre-images can be overwrittenthe pre-images can be overwritten even ifeven if
they are needed to provide a read-they are needed to provide a read-
consistent view to another query.consistent view to another query.
"Snapshot too old" simply means that pre-"Snapshot too old" simply means that pre-
images which the query needs to maintain aimages which the query needs to maintain a
read-consistent view have been overwritten.read-consistent view have been overwritten.
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
21/31
Common recommendationsCommon recommendations
Common recommendations to reduce the possibility ofCommon recommendations to reduce the possibility of"snapshot too old" are:"snapshot too old" are:
Keep transactions as fast as possibleKeep transactions as fast as possible
Increase the size/number of rollback segmentsIncrease the size/number of rollback segments
DoDo notnotspecify an OPTIMAL size for your rollbackspecify an OPTIMAL size for your rollback
segments.segments.
Increase the size of UNDO_RETENTION parameterIncrease the size of UNDO_RETENTION parameter
(amount of committed undo information to retain in the database)(amount of committed undo information to retain in the database)
Avoid executing long-running queries when transactionsAvoid executing long-running queries when transactions
which update the table are also executing.which update the table are also executing.
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
22/31
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
23/31
Serializable Transaction FailureSerializable Transaction Failure
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
24/31
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
25/31
DeadlockDeadlock
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
26/31
Types of LocksTypes of Locks
Internal locks and latches
protect internal database
structures such as datafiles
Internal locks and latches
DDL locks protect the
structure of schema objects
DDL locks (dictionary locks)
DML locks protect data
For example, table locks lock
entire tables, rowlocks lockselected rows.
DML locks (data locks)
DescriptionLock
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
27/31
Table LocksTable Locks
RS: row shareRS: row share
RX: row exclusiveRX: row exclusive
S: shareS: share
SRX: share rowSRX: share row
exclusiveexclusive
X: exclusiveX: exclusive
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
28/31
Data Lock EscalationData Lock Escalation
A transaction holds exclusive row locks forA transaction holds exclusive row locks forall rows inserted, updated, or deleted withinall rows inserted, updated, or deleted within
the transaction. Because row locks arethe transaction. Because row locks are
acquired at the highest degree ofacquired at the highest degree ofrestrictiveness, no lock conversion isrestrictiveness, no lock conversion is
required or performed.required or performed.
Oracle automatically converts a table lockOracle automatically converts a table lockof lower restrictiveness to one of higherof lower restrictiveness to one of higher
restrictiveness as appropriaterestrictiveness as appropriate
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
29/31
MS SQL Isolation LevelsMS SQL Isolation Levels
SERIALIZABLESNAPSHOT
No EquivalentREAD UNCOMMITTED
No EquivalentREPEATABLE READ
No EquivalentREAD COMMITTED with locking
No EquivalentSERIALIZABLE
READ ONLYSNAPSHOT
READ COMMITTEDREAD COMMITTED with snapshots
SELECT... FOR UPDATESELECT ... WITH (UPDLOCK)
OracleMicrosoft SQL Server 2005
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
30/31
DB2 Isolation LevelsDB2 Isolation Levels
Isolation levelsIsolation levels
Repeatable Read (RR)Repeatable Read (RR)
Read Stability (RS)Read Stability (RS)
Cursor Stability (CS)Cursor Stability (CS)
Uncommitted Read (UR)Uncommitted Read (UR)
Levels of lockingLevels of locking
TablespaceTablespace
TableTable
RowRow
8/14/2019 Transactions in Oracle Evgenya Kotzeva[1]
31/31
Transactions in OracleTransactions in Oracle
??