Docsity

Transaction Management-Distributed Database-Lecture 08 Slides-Computer Science, Appunti di Basi di Dati

Anteprima parziale del testo

Scarica Transaction Management-Distributed Database-Lecture 08 Slides-Computer Science e più Appunti in PDF di Basi di Dati solo su Docsity!

Chapter 8: Introduction to Transaction

Management

Definition and Examples

Properties

Classification

Processing Issues

Acknowledgements:

I am indebted to Arturas Mazeika for providing me his slides of this course.

J. Gamper

Definition

Transaction:

A collection of actions that transforms the DB from one consistent state

into another consistent state; during the exectuion the DB might be inconsistent.

J. Gamper

Example

Example:

Consider an SQL query for increasing by 10% the budget of the CAD/CAM

transaction and inserting a begin and end tag.project. This query can be specified as a transaction by providing a name for the

Transaction

BUDGET_UPDATE

begin

UPDATEEXEC SQL

PROJ

SET

BUDGET =

BUDGET * 1.

WHERE

PNAME =

"CAD/CAM"

end.

J. Gamper

Example...

Example:

Consider an airline DB with the following relations:

FLIGHT(FNO, DATE

,

SRC, DEST, STSOLD, CAP)

CUST(CNAME

, ADDR, BAL)

FC(FNO, DATE, CNAME

, SPECIAL)

date, and a customer name, and then asks for a reservation.Consider the reservation of a ticket, where a travel agent enters the flight number, the

Begin

transaction

Reservation

begin

input

(flight_no, date, customer_name);

EXEC SQL UPDATE

FLIGHT

SET

STSOLD =

STSOLD +

1

WHERE

FNO

= flight_no AND DATE =

date;

EXEC SQL INSERT

INTO

FC(FNO, DATE, CNAME, SPECIAL);

VALUES

(flight_no, date, customer_name, null);

output

("reservation completed")

end

.

J. Gamper

Example...

Transactions are mainly characterized by its Read and Write operations

Read set (RS): The data items that a transaction reads

Write set (WS): The data items that a transaction writes

Base set (BS): the union of the read set and write set

Example (contd.):

Read and Write set of the “Reservation” transaction

RS[Reservation]

=

FLIGHT.STSOLD, FLIGHT.CAP

WS[Reservation]

=

FC.CNAME, FC.SPECIALFLIGHT.STSOLD, FC.FNO, FC.DATE,

BS[Reservation]

=

FC.FNO, FC.DATE, FC.CNAME, FC.SPECIALFLIGHT.STSOLD, FLIGHT.CAP,

J. Gamper

Formalization of a Transaction

We use the following notation:

T

i be a transaction and

x

be a relation or a data item of a relation

O

ij

R

x ) , W

x ) }

be an atomic

read

/ write

operation of

T

i on data item

x

OS

i

=

j O

ij

be the set of all operations of

T

i

N

i ∈ {

A, C

be the termination operation, i.e.,

abort/commit

Two operations

O

ij (^) ( x )

and

O

ik

( x )

on the same data item are in

conflict

if at least one

of them is a

write

operation

A

transaction

T

i is a

partial order

over its operations, i.e.,

T i = { Σ i ,

i } , where

i

OS

i ∪

N

i

For any

O

ij

= { R ( x ) ∨ W

x ) }

and

O

ik

W

x ) , either

O

ij

i

O

ik

or

O

ik

i

O

ij

O

ij

OS

i ( O

ij

i N

i )

Remarks

The partial order

is given and is actually application dependent

It has to specify the

execution order

between the conflicting operations and between

all operations and the termination operation

J. Gamper

Formalization of a Transaction...

Example (contd.):

A transaction can also be specified/represented as a directed acyclic

graph (DAG), where the vertices are the operations and the edges indicate the ordering.

Assume

≺ = { ( R ( x )

, W

x ))

, (^) ( R

( y ) , W

x ))

, (^) ( W

x ) , C

R

x ) , C

R

y ) , C

The DAG is

J. Gamper

Formalization of a Transaction...

Example:

The reservation transaction is more complex, as it has two possible

termination conditions, but a transaction allows only one

BUT, a transaction is the

execution

of a program which has obviously only one

termination

Thus, it can be represented as two transactions, one that aborts and one that commits

Transaction T1: Σ =

{ R

( ST SOLD

) , R

( CAP

(^) ) , A

}

≺ = { ( R (

ST SOLD

) , A

) , (^) ( R

( CAP

(^) ) , A

) }

Σ = Transaction T2:

{ R

( ST SOLD

) , R

( CAP

(^) ) ,

W

(^) ( ST SOLD

) , W

(^) ( F N O

) , W

(^) ( DAT E

) ,

W

(^) ( CN AM E

) , W

(^) ( SP ECIAL

) , C

}

≺ = { ( R (

ST SOLD

) , W

(^) ( ST SOLD

))

,...

(^) }

Begin

(^) transaction

Reservation

begin

input

(flight

(^) no,

date, customer

name);

EXEC SQL SELECT

STSOLD,CAP

INTO

temp1,temp

FROM

FLIGHT

WHERE

FNO

= flight

no

AND

DATE

= date;

if temp1 =

temp

then

output

("no

free

seats");

Abort

else

EXEC SQL UPDATE

FLIGHT

SET

STSOLD

= STSOLD

  • 1

WHERE

FNO

= flight

no

AND

DATE =

date;

EXEC SQL INSERT

INTO

FC(FNO,

DATE, CNAME,

SPECIAL);

VALUES

(flight

no, date,

customer

(^) name,

null);

output Commit

("reservation

completed")

endif

end

.

J. Gamper

Page 11

Properties of Transactions...

Atomicity

Either

all or none

of the transaction’s operations are performed

Partial results of an interrupted transactions must be undone

  • Transaction recovery

is the activity of the restoration of atomicity due to input errors,

system overloads, and deadlocks

  • Crash recovery

is the activity of ensuring atomicity in the presence of system

crashes

J. Gamper

Properties of Transactions...

Consistency

transaction transforms a consistent DB into a consistent DBThe consistency of a transaction is simply its correctness and ensures that a

Transactions are

correct

programs and do not violate database integrity constraints

  • Dirty data

is data that is updated by a transaction that has not yet committed

Different

levels of DB consistency

(by Gray et al., 1976)

· Degree 0

Transaction

T

does not overwrite dirty data of other transactions

· Degree 1

Degree 0 +

T

does not commit any writes before EOT

· Degree 2

Degree 1 +

T

does not read dirty data from other transactions

· Degree 3

Degree 2 + Other transactions do not dirty any data read by

T

before

T

completes

J. Gamper

Properties of Transactions...

Example:

Consider the following two transactions, where initially

x

:

T1: Read(x)

x

x+

CommitWrite(x)

T2: Read(x)

x

x+

CommitWrite(x)

Possible execution sequences:

T1: x T1: Read(x)

←

x+

T2: xT2: Read(x)T1: CommitT1: Write(x)

←

x+

T2: CommitT2: Write(x)

sultSerial execution: we get the correct re-

x

(the same for

T

2 , T

1 } )

T1: x T1: Read(x)

←

x+

T2: xT1: Write(x)T2: Read(x)

←

x+

T2: CommitT1: CommitT2: Write(x)

Concurrent execution:

T

2

reads the

value of

(^) x

while it is being changed; the

result is

x

and is incorrect!

J. Gamper

Properties of Transactions...

SQL-92 specifies 3 phenomena/situations that occur if proper isolation is not maintained

  • Dirty read

∗

T

1

modifies

x

which is then read by

T

2

before

T

1

terminates; if

T

1

aborts,

T

2

has

read value which never exists in the DB:

  • Non-repeatable (fuzzy) read

∗

T

1

reads

x ; T 2

then modifies or deletes

x

and commits;

T

1

tries to read

x

again

but reads a different value or can’t find it

  • Phantom

∗

T

1

searches the database according to a predicate

P

while

T

2

inserts new tuples

that satisfy

P

J. Gamper

Properties of Transactions...

Durability

operations will never be lost, in spite of subsequent failuresOnce a transaction commits, the system must guarantee that the results of its

Database recovery is used to achieve the task

J. Gamper

Classification of Transactions

Classification

of transactions according to various criteria

  • Duration

of transaction

On-line (short-life)

Batch (long-life)

  • Organization

of

read

and

write

instructions in transaction

General model

T 1 : { R ( x )

, R

y ) , W

y ) , R

z ) , W

x ) , W

z ) , W

w

) , C

Two-step (all reads before writes)

T 2 : { R ( x )

, R

y ) , R

z ) , W

x ) , W

z ) , W

y ) , W

w

) , C

Restricted (a data item has to be read before an update)

T 3 : { R ( x )

, R

y ) , W

y ) , R

z ) , W

x ) , W

z ) , (^) R

w

, W

w

) , C

Action model: each (read,write) pair is executed atomically

T 2 : { [ R ( x )

, W

x )]

, (^) [ R

( y ) , W

y )]

, (^) [ R

( z ) , W

z )]

, (^) [ R

( w

) , W

w

)]

, C

J. Gamper