You are on page 1of 48

OLTP

Online Transaction Processing


Education and Research Dept Infosys Technologies Limited, Bangalore

On Line Transaction Processing

What is a Transaction ? A Logical unit of work Examples: Drawing Money from a bank account Booking a seat on an airline

Transactions
It is a unit of program execution that accesses & possibly updates various data items. A transaction is a logical unit of work that performs some useful function for a user. In end of the transaction the system must be: in the prior state (if the transaction fails) or the status of the system should reflect the successful completion (if the transaction succeeded). May take a database from one consistent state to another.

Characteristics of Transactions

Atomicity Consistency Isolation Durability

Atomicity

r s ctio s r i i isi l . All or No t s ts t c s t tr s ctio is com l t (if t tr s ctio is s cc ssf l) or ot t if t tr s ctio f ils.

Consistency(Integrity)

e t se ill o e fro one consistent st te to not er if t e tr ns ction s ccee s n re in in t e origin l st te if t e tr ns ction f ils. ec tion of tr ns ction in isol tion reser es t e consistency of t e t se.

Isolation

r transaction s o l o rat as if it is onl transaction in t s st .

For r air of transaction 1 an 2, it ars to 1 t at it r 2 finis s a c tion for 1start , or 1 finis c tion for 2 start .

Durability

c a tra sacti as c l t succ ssfully, t u at tu l s ust b a a ailabl f r all t r tra sacti s r a t basis. I t r r s, aft r a tra sacti c l t s s it as a t t succ ssfully, t c a atabas rsists, if t r ar syst failur s.

Boundary of a transaction
B S U U GIN NS C I N l ct dat dat B CK NS C I N or C

NS C I N

B GIN i li s t start of a transaction Succ ssful transaction is d not d y I C Failur of a transaction is si nal d y B CK

Transaction state
A transaction must be in one of the following states: Active: the transaction is executing; Partially committed: after the final statement has been executed; ailed: after the discovery that normal execution can no longer proceed; Aborted: after the transaction is rolled back & the DB restored; Committed: after successful completion

Committed Transaction

A transaction that has successfully completed is said to have committed. A committed Transaction that performs updates transforms the database into a new consistent state

Aborted/Rolled Back Transaction

A transaction that has not completed its execution successfully is said to have Aborted. An aborted transaction must have no effect on the state of the database. Recovery scheme must take care of Aborted transactions.

State diagram of a transaction


completed

committed

active

failed

aborted

A transaction enters the failed state after the system determines that the transaction can no longer proceed with its normal execution. Such transaction are rolled back & aborted. At this point the system has two options: restart the transaction or kill the transaction.

On-line Transaction

soft are s ste t at reflects t e transactions of a real life s ste in t eir entiret is calle a Transaction rocessin (T ) s ste . T s ste t at ro i es on-line ser ice or instantaneo s access is calle an On-line T s ste or O T s ste .

Features of OLTP system

Se eral co curre t tra sactio s are i itiate from s atially istri ute termi als. ac tra sactio as t e ee for e ecuti se eral mac i e i structio s, retrie als, u ates, a se i or recei i messa es. Processors istri ute eo ra ically e ecute t e ro rams i itiate y t ese tra sactio s. T ere may e o e or more ata ases ic may e s atially istri ute a use y t e tra sactio .

Features of OLTP System

Handle Spatially Distributed M/cs Execution of Instructions and Queries across LA /WA eographically distributed processors Spatially Distributed Databases

Responsibilities of OLTP system

Data validation Support concurrent (interleaved) transactions Recovery database at failure Database consistency / Integrity Security

Responsibilities of OLTP system


Must completely validate data before entry into the database so that the integrity is safeguarded. Must allow for concurrent (also called interleaved) execution of transactions (Concurrency). If the transaction fails due to any reason, there must be a recovery method to recover the database to the status as of the last completed transaction. System must ensure that integrity (consistency) of data at all times despite allowing interleaved transactions. There must be an effective security system to control access to the system

Online Transaction Processing


Security Access Control for the users by the system Integrity Validation data before entering it into DB. . Concurrency Allow concurrent(interleaved) transactions Recovery System failure/Transaction Failure should not cost the loss of data

Security
Protection of data against unauthorized disclosure, alteration or destruction. Access is allowed to only authorized users User identification - Authorized users connect to the database using user id and password. Views Access Privileges

Security Contd.. Privileges


rant all on emp to user rant select,update on emp to trng0 rant all on emp to enr_dba with grant option Revoke update on dept from trng 0

Security Contd.. Views


Create view enr_emp as (select emp#,name,dept,grade from employee where dept=enr) Create view fin_emp as (select emp#,name,acctno from employee)

Integrity
Integrity refers to the accuracy or validity of data.

Domain Integrity Entity Integrity Referential Integrity

Integrity
Domain Integrity -Check constraint
Used to specify a range of values that a column can take. Used to specify business rules on individual data items.

Integrity
Check Constraint
The constraint is employee location should be only one of BA ALORE , BOMBAY ,DELHI Create Table Employee ( Emp_no Integer ot ull, Varchar ( 5) , EMP_ AME EMP_A E Integer, EMP_LOC Varchar( 5), CHECK ( EMP_LOC in ( BA ALORE, Bombay,delhi)));

Integrity
Entity Integrity Primary key - ot ull, Unique Foreign key

Integrity
Referential Integrity Foreign key values Update anomalies possible

Integrity
Referential Integrity
A row is inserted into a child table Updating the foreign key in a child table Updating the primary key in a parent table Deleting a row in a parent table

Integrity
Referential Integrity
DELETE restrict cascade set null set default

Integrity
Example
create table ORDERS ( Order_ um Integer Order_Date Date Cust Integer Rep Integer, Mfr VarChar(3) Product Char (5) Qty Integer Amount Money

OT ULL, OT ULL, OT ULL, OT OT OT OT ULL, ULL, ULL, ULL,

Integrity
Example Cont...
Primary key (Order_ um) , Customers Foreign key (Cust) REFERE CES O DELETE CASCADE , SalesReps Foreign key (Rep) REFERE CES O DELETE SET ULL, O UPDATE CASCADE, Foreign key (Mfr,Product) REFERE CES Products O DELETE RESTRICT )

Default is RESTRICT

Concurrency

Introduction

Transactions seldom execute in isolation What happens when transactions execute concurrently?

Concurrent Transactions

Serializability Locking Deadlocks Time Stamping

Serial execution of two transactions


Trans T
A = 00 Read(A,a ) a =a -50 A = 50 Write(A,a ) B = 00 Read(B,b ) b =b +50 B = 50 Write(B,b ) A = 00 B = 00 A = 35 B = 65

Trans T

Read(A,a ) temp=a *0. a =a -temp Write(A,a ) read(B,b ) b =b +temp Write(B,b )

A = 50 A = 35 B = 50 B = 65

Serializability

A given interleaved execution of some set of transactions is said to be serializable if and only if it produces the same result as some serial execution of those transactions

Serialized execution
A = 00 A = 50 Read(A,a ) a =a -50 Write(A,a )

A = 00 B = 00
Read(A,a ) temp=a *0. a =a -temp Write(A,a )

A = 35 B = 65
A = 50 A = 35

B = 00 B = 50

Read(B,b ) b =b +50 Write(B,b ) Commit

Read(B,b ) b =b +temp Write(B,b ) commit

B = 50 B = 65

Problems in concurrent transactions

Lost Update Dirty Read Incorrect Summary Phantom Record

Lost update
A= 00 T A= 00 A= 00 A= 50
Read (A, a ) a = a + 50 write(A,a ) Commit

Time
t t t3 t4 t5 t6 t7 t8

Read (A, b ) A= 00

b =b * Write(A,b ) Commit

A= 00 A= 00

Lost update

The Updation done by transaction A is totally lost even before it is seen

Dirty Read
A=300 A= 00 T A= 00 A= 50 A= 00
Read(A,a ) a = a +50 Write(A,a )

Time
t t t3 t4 t5 t6 t7 t8

Read(A,b ) A= 50 b =b * Write(A,b ) A=300 Commit

Rollback

Dirty Read

Transaction T Updates record A at time t3 and then it decides to rollback or undo But Transaction T reads the updated data which is not the correct data, and does some Updation on the wrong data and commits.

Incorrect summary
T A= 00 Read (A,a )
a = a -50

Time
t t t3 t4 t5 t6 t7 t8 t9 t 0 t t

T
Sum = 0 Read(A,b )

A = 00 B = 00

Sum=350 A= 00

A=50

Write(A,a )

Sum = Sum + b

B= 00 Read(B,a )
a = a +50 B= 50 Write(B,a ) Commit

Sum= 00

B= 50 Read(B,b ) Sum = Sum + b Commit

Sum=350

Incorrect summary

Transaction T Updates record A and record B Transaction T which has to calculate the sum of Updated record, has read record A before Updation and Record B after Updation, resulting in Incorrect Summary or The transaction T has seen the database in an inconsistent state and has therefore performed an I CO SISTE T A ALYSIS

Phantom Record
TRA S-T Insert X Insert Y Time T T T3 T4 T5 T6 T7 T8 TRA S-T Sum = 0 Ret. Bal_X Sum=sum + Bal_X Ret. Y Sum=sum + Bal_Y COMMIT

Insert Z COMMIT

Phantom Record
Until TRA S-T COMMITS, the TRA S-T cannot see the existence of Z Thus, Z is a PHA TOM RECORD as far as TRA S-B is concerned Unless TRA S-T , prevents TRA S-T from inserting Z, the two transactions are not serializable

Phantom record
t t t3 t4 t5 t6 t7 t8 t9 t 0 t t Sum = 0 Read(A,b ) Sum = Sum + b Read(B,b ) Sum = Sum + b

Delete B

Commit

Commit

Concurrency Problems

These problems are overcome by LOCKS Time Stamping

You might also like