Sie sind auf Seite 1von 50

TRANSACTIONS

NEHA J MENDJOGE

DEFINITION
A transaction is a unit of program execution that accesses and possibly updates various data items. A transaction is initiated by a user program written in a high-level data manipulation language or any programming language like SQL,C++,JAVA etc.

NEHA J MENDJOGE

EXAMPLE

E.g. transaction to transfer $50 from account A to account B: 1. read(A) 2. A := A 50 3. write(A) 4. read(B) 5. B := B + 50 6. write(B)
NEHA J MENDJOGE

EXAMPLE

E.g. transaction to transfer $50 from account A to account B: Transfers data from the 1. read(A) database to a local buffer belonging to the 2. A := A 50 transaction that executed the read operation 3. write(A) 4. read(B) Transfers data from the local buffer of the 5. B := B + 50 transaction that executed the write operation back 6. write(B)
to the database
NEHA J MENDJOGE

ACID PROPERTIES OF A TRANSACTION


1. Atomicity:

Either all operations of the transaction are reflected properly in the database, or none are.
2. Consistency:

Execution of a transaction in isolation (i.e, with no other transaction executing concurrently) preserves the consistency of the database.
NEHA J MENDJOGE

3.

Isolation:

Each transaction is unaware of other transactions executing concurrently in the system.


4.

Durability:

After a transaction completes successfully, the changes it has made to the database persist, even if there are system failures.
NEHA J MENDJOGE

TRANSACTION STATE

NEHA J MENDJOGE

NEHA J MENDJOGE

Active the initial state; the transaction stays in this state while it is executing Partially committed after the final statement has been executed. Failed -- after the discovery that normal execution can no longer proceed. Aborted after the transaction has been rolled back and the database restored to its state prior to the start of the transaction. Two options after it has been aborted: restart the transaction: can be done only if hardware or software error. kill the transaction: It is usually done because of some internal logical error that can be corrected only by rewriting the application program.
NEHA J MENDJOGE Committed after successful completion.

IMPLEMENTATION OF ATOMICITY & DURABILITY OR SHADOW COPY TECHNIQUE

NEHA J MENDJOGE

Shadow Copy Technique


1. In the shadow-copy scheme, a transaction that wants to update the database first creates a complete copy of the database.

2.All updates are done on the new database copy, leaving the original copy, the shadow copy, untouched. If at any point the transaction has to be aborted, the system merely deletes the new copy. The old copy of the database has not been affected.
NEHA J MENDJOGE

NEHA J MENDJOGE

TRANSACTION FAILURE: If transaction fails before db-pointer is updated: If transaction fails after db-pointer is updated:

SYSTEM FAILURE If the system fails before the updated dbpointer is written to the disk: If the system fails after the updated dbpointer is written to the disk:
NEHA J MENDJOGE

Types of Failures
(i) Logical error: In this case the transaction can no longer continue with its normal execution because of some internal condition such as bad input, data not found or overflow. Eg: infinite loop in a program (ii) System error: In this case the system enters an undesirable state. Eg: system hangs
NEHA J MENDJOGE

(iii) System crash: In this case hardware malfunctions or there may be bugs in the database software or the operating system causes the loss of data in volatile storage. (iv) Disk failure: In this case a disk block loses its contents as a result of either a head crash or failure during data transfer operation. Eg: CD or pendrive is corrupted
NEHA J MENDJOGE

CONCURRENT EXECUTION
Allowing

multiple transactions to access, modify data at the same time i.e. CONCURRENTLY. The number of transactions executed in a given amount of time. TIME: The amount of time required to complete a task from the time it was submitted.
NEHA J MENDJOGE

THROUGHPUT:

RESPONSE

ADVANTAGES OF CONCURRENT ACCESS

IMPROVED THROUGHPUT & RESOURCE UTILIZATION: one transaction can be using the CPU while another is reading from or writing to the disk

REDUCED WAITING TIME: short transactions need not wait behind long ones.
NEHA J MENDJOGE

T1 transfer $50 from A to B

T2 transfer 10% of the balance from A to B

Read(A); A:=A-50; Write(A); Read(B); B:=B+50; Write(B);

Read(A); Temp:=A*0.1; A:=A-temp; Write(A); Read(B); B:=B + temp; Write(B);

NEHA J MENDJOGE

T1

T2

Read(A); A:=A-50; Write(A); Read(B); B:=B+50; Write(B);

1000 950 950 2000 2050 2050

Read(A); Temp:=A*0.1; A:=A-temp; Write(A); Read(B); B:=B + temp; Write(B);

NEHA J MENDJOGE

T1

T2

Read(A); A:=A-50; Write(A); Read(B); B:=B+50; Write(B);

1000 950 950 2000 2050 2050

Read(A); Temp:=A*0.1; A:=A-temp; Write(A); Read(B); B:=B + temp; Write(B);

950 95 855 855 2050 2145 2145

NEHA J MENDJOGE

T1

T2

Read(A); A:=A-50; Write(A); Read(B); B:=B+50; Write(B);

1000 950 950 2000 2050 2050

Read(A); Temp:=A*0.1; A:=A-temp; Write(A); Read(B); B:=B + temp; Write(B);

950 95 855
855

2050 2145
2145

NEHA J MENDJOGE

T1

T2

Read(A); Temp:=A*0.1; A:=A-temp; Write(A); Read(B); B:=B + temp; Write(B);

1000 100 900 900 2000 2100 2100

Read(A); A:=A-50; Write(A); Read(B); B:=B+50; Write(B);

900 850 850 2100 2150 2150


NEHA J MENDJOGE

T1

T2

Read(A); Temp:=A*0.1; A:=A-temp; Write(A); Read(B); B:=B + temp; Write(B);

1000 100 900 900 2000 2100 2100

Read(A); A:=A-50; Write(A); Read(B); B:=B+50; Write(B);

900 850
850

2100 2150
2150
NEHA J MENDJOGE

SCHEDULES

the chronological order in which instructions of concurrent transactions are executed.


a schedule for a set of transactions must consist of all instructions of those transactions must preserve the order in which the instructions appear in each individual transaction.

NEHA J MENDJOGE

SERIAL SCHEDULE
Consists of a sequence of instructions from various transactions where the instructions belonging to one single transaction appear together in that schedule. For a set of n transactions there can be n! different valid serial schedules.

NEHA J MENDJOGE

The following schedule is not a serial schedule, but it is equivalent to the previous Schedule. T1 T2

Read(A); A:=A-50; Write(A);

1000 950 950

Read(A); Temp:=A*0.1; A:=A-temp; Write(A);

950 95 855
855

Read(B); B:=B+50; Write(B);

2000 2050 2050

Read(B); B:=B + temp; Write(B);


NEHA J MENDJOGE

2050 2145
2145

T1

T2

Read(A); A:=A-50;

1000 950

Read(A); Temp:=A*0.1; A:=A-temp; Write(A); Read(B);

1000 100 900 900 2000

Write(A); Read(B); B:=B+50; Write(B);

950 2000 2050 2050

B:=B + temp; Write(B);


NEHA J MENDJOGE

2100
2100

SERIALIZABILITY
Each transaction preserves database consistency. Thus serial execution of a set of transactions preserves database consistency. A (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule. Different forms of schedule equivalence give rise to the notions of: 1. conflict serializability 2. view serializability

NEHA J MENDJOGE

CONFLICT SERIALIZABILITY

NEHA J MENDJOGE

Instructions li and lj of transactions Ti and Tj

respectively, conflict if and only if there exists


some item Q accessed by both li and lj, and at least one of these instructions wrote Q. 1. li = read(Q), lj = read(Q). li and lj dont conflict. 2. li = read(Q), lj = write(Q). They conflict.

3. li = write(Q), lj = read(Q). They conflict.


4. li = write(Q), lj = write(Q). They conflict.
NEHA J MENDJOGE

We say that li and lj conflict if they are operations by different transactions on the same data item and atleast one of these instructions is a write operation.

NEHA J MENDJOGE

Consider the following schedule X T1 T2

Read(A); A:=A-50; Write(A);


CONFLICT

Read(A); Temp:=A*0.1; A:=A-temp; Write(A);

Read(B); B:=B+50; Write(B);

CONFLICT

Read(B); B:=B + temp; Write(B);


NEHA J MENDJOGE

Lets try to make this a serializable schedule T1 T2

Read(A); Write(A);

Read(A); Write(A);

Read(B); Write(B);

Read(B); Write(B);

NEHA J MENDJOGE

Lets try to make this a serializable schedule T1 T2

Read(A); Write(A);

Read(B);

Read(A); Write(A);

Write(B);

Read(B); Write(B);

NEHA J MENDJOGE

Lets try to make this a serializable schedule T1 T2

Read(A); Write(A); Read(B);

Read(A); Write(A);

Write(B);

Read(B); Write(B);

NEHA J MENDJOGE

Lets try to make this a serializable schedule T1 T2

Read(A); Write(A); Read(B);

Read(A); Write(A);

Write(B);

Read(B); Write(B);

NEHA J MENDJOGE

Lets try to make this a serializable schedule T1 T2

Read(A); Write(A) Read(B); Write(B); Read(A); Write(A);

Read(B); Write(B);
The above schedule is equivalent to Schedule X

NEHA J MENDJOGE

CONFLICT EQUIVALENT SCHEDULE

If a schedule S can be transformed into a schedule S by a series of swaps of non-conflicting instructions, we say that S and S are conflict equivalent. We say that a schedule S is conflict serializable if it is conflict equivalent to a serial schedule.

NEHA J MENDJOGE

VIEW SERIALIZABILITY

NEHA J MENDJOGE

Let S and S be two schedules with the same set of transactions. S and S are view equivalent if the following three conditions are met, for each data item Q, 1. If in schedule S, transaction Ti reads the initial value of Q, then in schedule S also transaction Ti must read the initial value of Q. 2. If in schedule S transaction Ti executes read(Q), and that value was produced by transaction Tj (if any), then in schedule S also transaction Ti must read the value of Q that was produced by the same write(Q) operation of transaction Tj . 3. The transaction (if any) that performs the final write(Q) operation in schedule S must also perform the final write(Q) operation in schedule S.

NEHA J MENDJOGE

Consider the two schedules given below. Are they view equivalent?
Schedule S Schedule S

NEHA J MENDJOGE

Consider the two schedules given below. Are they view equivalent?
Schedule S Schedule S

NEHA J MENDJOGE

A schedule S is view serializable if it is view equivalent to a serial schedule.

NEHA J MENDJOGE

TESTING FOR SERIALIZABILITY

NEHA J MENDJOGE

We first construct a directed graph called as PRECEDENCE GRAPH. This graph consists of a pair G=(V,E) V= is a set of vertices(i.e. all the transactions) E= is a set of edges The set of edges consists of all edges Ti Tj for which one of the three condition holds: 1. Ti executes write(Q) before Tj executes read(Q) 2. Ti executes read(Q) before Tj executes write(Q) 3. Ti executes write(Q) before Tj executes write(Q)

NEHA J MENDJOGE

Consider the given schedule 1:

T1

T2

NEHA J MENDJOGE

Consider the given schedule 2:

T2

T1

NEHA J MENDJOGE

Consider the given schedule


T1T2 :T1 executes read(A) before T2 executes write(A) T2 T1: T2 executes read(B) before T1 executes write(B)

T1

T2

NEHA J MENDJOGE

Is the given schedule conflict serializable???

NEHA J MENDJOGE

Is the given schedule conflict serializable???

NO

If the precedence graph for the schedule contains a cycle then that schedule is NOT
CONFLICT SERIALIZABLE.

NEHA J MENDJOGE

Das könnte Ihnen auch gefallen