Sunteți pe pagina 1din 38

OLTP

Transaction ?
End Users

DataBase
communication End User view point: A request/reply unit System view point:A sequence of operations( Read, write, etc)

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.
DataBase in consistent state Before Transaction DataBase in consistent state After Transaction

Properties
 Atomicity  Consistency  Isolation  Durability

Boundary
BEGIN TRANSACTION Statement to execute the transaction COMMIT/ROLLBACK TRANSACTION

OnOn-line Transaction
A software system that reflects the transactions of a real life system in their entirety is called a Transaction Processing (TP) system. A TP system that provides on-line (where the user is in touch with the system through a terminal or a PC) or instantaneous access is called an On-line TP system or OLTP system.

Features
Concurrent transactions initiated spatially distributed terminals. from

Processors distributed geographically execute the programs initiated by these transactions. One or more databases which may be spatially distributed and used by the transactions.

Responsibilities


Data validation before making entry into the database

Allow concurrent execution of transactions and maintain consistency of data

Recover database in case of transaction failure Secured control access to the system

Requirements of OLTP
Security Integrity Concurrency Recovery

Security
Protection of data against unauthorized disclosure, alteration or destruction.

User identification - authorization

Security using SQL


GRANT REVOKE CREATE Views

Integrity
Integrity refers to business rules of the enterprise
measure of correctness validity of individual items interrelationship between data items in the DataBase at a given point of time interrelationship between data items across all points of time (before start or after completion or cancellation of a transaction);

Type of Integrity Constraints


There are three types of integrity constraint generally imposed on an OLTP system: Domain Integrity Entity Integrity Referential 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.
An employees age must be at least 18 years and not more than 65 years. CHECK ( emp_age >= 18 AND emp_age <= 65)

Check Constraint
Constraint: employee location should be only one of BANGALORE, BOMBAY, DELHI CREATE TABLE EMPLOYEE ( EMP_NO integer NOT NULL, EMP_NAME varchar (15) , EMP_AGE integer, EMP_LOC varchar(15), CHECK ( EMP_LOC in ( BANGALORE, BOMBAY,DELHI)));

Entity Integrity
Entity integrity refers to the fact that theres only one physical entity corresponding to a particular attribute. This attribute is the Primary key.

Primary key - cannot be Null - should be Unique

Referential Integrity
Every employee of any organization must belong to a valid department. Thus, the dept_no field (defined as a foreign key) in the EMPLOYEE table must be a valid key in the DEPARTMENT table. This is called Referential Integrity.

Effects of 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. What happens?

Deleting a row in parent table


Options can be set in CREATE table command: ON DELETE RESTRICT (Default) ON DELETE SET NULL ON DELETE SET CASCADE

Example of deleting (integrity)


CREATE table ORDERS ( Order_Num Integer NOT NULL, Order_Date Date NOT NULL, Cust Integer NOT NULL, Rep Integer Integer Mfr Char(3) NOT NULL, Product Char (5) NOT NULL, Qty Integer Integer NOT NULL, Amount Money NOT NULL,

Example of deleting...
Primary key (Order_Num), Foreign key (Cust) REFERENCES Customers ON DELETE CASCADE, Foreign key (Rep) REFERENCES SalesReps ON DELETE SET NULL, ON UPDATE CASCADE, Foreign key (Mfr, Product) REFERENCES Products ON DELETE RESTRICT )

Concurrency

A=1000 B=2000

Serial execution of two transactions


T2
Read(A,a1) a1=a1-50 Write(A,a1) Read(B,b1) b1=b1+50 Write(B,b1)

T1

Read(A,a2) temp=a2*0.1 a2=a2-temp Write(A,a2) read(B,b2) b2=b2+temp Write(B,b2)

Serial execution
Serial means executing a set of transactions one after another with no interaction among them at all. Due to the multiprogramming facility the transactions do not get executed serially but execute in an interleaved manner but produce the same result as serial execution of transactions. But How?

A possible concurrent schedule


T1 Read(A,a1) a1=a1-50 T2 Read(A,a2) temp=a2*0.1 a2=a2-temp Write(A,a2) Read(B,b2)

Write(A,a1) Read(B,b1) b1=b1+50 Write(B,b1)

b2=b2+temp Write(B,b2)

Serialized execution of the same transactions


T1 Read(A, a1) a1=a1-50 Write(A, a1) T2 Read(A, a2) temp=a2*0.1 a2=a2-temp Write(A, a2)

Read(B, b1) b1=b1+50 Write(B, b1)

Read(B, b2) b2=b2+temp Write(B, b2)

Problems when Transactions execute concurrently


Lost Update Dirty Read Incorrect Summary Phantom Record

A=20

Lost update
Transacn T2 t1 t2 t3 t4 t5 t6 t7 t8

Transacn T1 Read (A, a1) a1 = a1 + 50 Write( A, a1) Commit

Read (A, b1)

b1 = b1 * 2 Write( A, b1) Commit

A=20

Dirty Read
Transacn T2 t1 t2 t3 t4 t5 t6 t7 t8

Transacn T1 Read(A, a1) a1 = a1+5 Write(A, a1)

Read(A, b1) b1 = b1 * 20 Write(A, b1) Commit

Rollback

A = 200 B = 100

Incorrect Summary
T2
t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 Read(A, b1) Sum = Sum + b1

T1
Read (A, a1) a1 = a1-100 Write (A, a1) Read(B, a2) a2 = a2 +100 Write (B, a2) Commit

Read (B, b2) Sum = Sum + b2 Commit

A = 200 B = 100 T1 C = 50 Read (A, a1) a1 = a1-100 Write(A, a1)

Phantom record
t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13

T2 Sum = 0

Read(A, b1) Sum = Sum + b1

Read(B, a2) a2 = a2 +100 Write(B, a2)

Read(B, b2) Sum = Sum + b2 Commit

Insert Commit

Concurrent Transactions
Concurrency control techniques : to achieve Serializability Locking Timestamping

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
Serializability is mandatory in the design of any OLTP system and the objective of any OLTP is to ensure serializability in the execution of interleaved (concurrent) transactions.

Locking
Locking mechanism records the allocation of a resource in a system table called Lock table. Whenever, a request for accessing a resource is issued, the system checks the Lock table for its availability.

Granularity of locking
Various resources : Field/Column Data row or a tuple Table Database

Locks
Shared Locks Exclusive Locks

Deadlock
Occurs when two or more separate processes compete for resources held by one another.

T1

T2

Deadlock
T1 lock-X(r1) update r1 t1 t2 t3 t4 t5 t4 t5 t6 T2 lock-X(r2) update r2

lock-X(r2) update r2

lock-X(r1) update r1

S-ar putea să vă placă și