

















Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!


















Definition and Examples
Properties
Classification
Processing Issues
Acknowledgements:
I am indebted to Arturas Mazeika for providing me his slides of this course.
J. Gamper
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:
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:
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
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
We use the following notation:
i be a transaction and
x
be a relation or a data item of a relation
ij
x ) , W
x ) }
be an atomic
read
/ write
operation of
i on data item
x
i
=
j O
ij
be the set of all operations of
i
i ∈ {
be the termination operation, i.e.,
abort/commit
Two operations
ij (^) ( x )
and
ik
( x )
on the same data item are in
conflict
if at least one
of them is a
write
operation
A
transaction
i is a
partial order
over its operations, i.e.,
T i = { Σ i ,
i } , where
i ∪
i
For any
ij
= { R ( x ) ∨ W
x ) }
and
ik
x ) , either
ij
i
O
ik
or
ik
i
O
ij
ij
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
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 )
x ))
, (^) ( R
( y ) , W
x ))
, (^) ( W
x ) , C
x ) , C
y ) , C
The DAG is
J. Gamper
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
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
Atomicity
Either
all or none
of the transaction’s operations are performed
Partial results of an interrupted transactions must be undone
is the activity of the restoration of atomicity due to input errors,
system overloads, and deadlocks
is the activity of ensuring atomicity in the presence of system
crashes
J. Gamper
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
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
does not overwrite dirty data of other transactions
· Degree 1
Degree 0 +
does not commit any writes before EOT
· Degree 2
Degree 1 +
does not read dirty data from other transactions
· Degree 3
Degree 2 + Other transactions do not dirty any data read by
before
completes
J. Gamper
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
2 , T
1 } )
T1: x T1: Read(x)
←
x+
T2: xT1: Write(x)T2: Read(x)
←
x+
T2: CommitT1: CommitT2: Write(x)
Concurrent execution:
2
reads the
value of
(^) x
while it is being changed; the
result is
x
and is incorrect!
J. Gamper
SQL-92 specifies 3 phenomena/situations that occur if proper isolation is not maintained
∗
1
modifies
x
which is then read by
2
before
1
terminates; if
1
aborts,
2
has
read value which never exists in the DB:
∗
1
reads
x ; T 2
then modifies or deletes
x
and commits;
1
tries to read
x
again
but reads a different value or can’t find it
∗
1
searches the database according to a predicate
while
2
inserts new tuples
that satisfy
J. Gamper
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 according to various criteria
of transaction
On-line (short-life)
Batch (long-life)
of
read
and
write
instructions in transaction
General model
T 1 : { R ( x )
y ) , W
y ) , R
z ) , W
x ) , W
z ) , W
w
) , C
Two-step (all reads before writes)
T 2 : { R ( x )
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 )
y ) , W
y ) , R
z ) , W
x ) , W
z ) , (^) R
w
w
) , C
Action model: each (read,write) pair is executed atomically
T 2 : { [ R ( x )
x )]
, (^) [ R
( y ) , W
y )]
, (^) [ R
( z ) , W
z )]
, (^) [ R
( w
) , W
w
)]
, C
J. Gamper