Professional Documents
Culture Documents
ADVANCED
DATABASE
SYSTEMS
TRANSACTION MANAGEMENT
By the end of this presentation you will be able
to do the following:
-Define what a transaction is
-Describe the states that a transaction goes
through from the time that it is initiated up to
the time that it is completed
-Describe the properties of a transaction
TRANSACTION DEFINED AS:
• A collection of operations that must be
processed as one unit of work
• A logical unit of work on a database
– An entire program
– A portion of a program
– A single command
• The entire series of steps necessary to
accomplish a logical unit of work
• A transaction is seen by the DBMS as a series,
or list, of actions
– Includes read and write of objects
– We’ll write this as R(o) and W(o) (sometimes RT(o)
and WT(o) )
• For example
T1: [R(a), W(a), R(c), W(c)]
T2: [R(b), W(b)]
• In addition, a transaction should specify as its
final action either commit, or abort
example
START TRANSACTION
Display greeting
Get account number, pin, type, and amount
SELECT account number, type and balance
If balance is sufficient then
UPDATE account by posting debit
UPDATE account by posting credit
INSERT history record
Display final message and issue cash
Else
Write error message
End If
On Error: ROLLBACK
COMMIT
ROLLBACK ROLLBACK
READ, WRITE
terminated
failed
If a system failure occurs, searching the log and rollback the transactions that
have written into the log a
[start_transaction, transaction-id]
[write_item, transaction-id, X, old_value, new_value]
but have not recorded into the log a [commit, transaction-id]
CONDITIONS FOR A TRANSACTION TO BE IN A
PARTIALLY COMMITTED STATE OR FAILED STATE
PARTIALLY COMMITTED STATE: A transaction is
partially committed if it has violated serializability
or integrity constraints or secondary storage failure
for record updations then the transaction has to be
aborted.
If the transaction has been successful any updates
can be safely recorded and the transaction can go
to the committed state.
FAILED STATE: It occurs if the transaction cannot
be committed or the transaction is aborted
while in the active state perhaps due to the user
aborting the transaction or as a result of the
concurrency control protocol aborting the
transaction to ensure the serializability
PROPERTIES OF A TRANSACTION
ATOMICITY PROPERTY
-It means that a transaction can not be sub-divided; either all the work in the
transaction is completed or nothing is done
• E.g. transaction to transfer $50 from account A to account B:
1. read_from_account(A)
2. A := A – 50
3. write_to_account(A)
4. read_from_account(B)
5. B := B + 50
6. write_to_account(B)
• Atomicity requirement
– if the transaction fails after step 3 and before step 6, money will be “lost” leading to an
inconsistent database state
• Failure could be due to software or hardware
– the system should ensure that updates of a partially executed transaction are not
reflected in the database
• A DBMS ensures atomicity by undoing the
actions of partial transactions (referred to as
backward recovery or rollback)
• Backward recovery is used to reverse the
changes made by transaction that have
aborted or terminated abnormally
• The component of the DBMS responsible for
this is called the recovery manager
CONSISTENCY PROPERTY
- It means that if applicable constraints are true
before the transaction starts, the constraints will
be true after the transaction terminates
- Each user is responsible to ensure that their
transaction (if executed by itself) would leave the
database in a consistent state e.g. if a user’s
account is balanced before a transaction then the
account is balanced after the transaction.
Otherwise the transaction is rejected and no
changes take effect
ISOLATION PROPERTY
Transactions are isolated, or protected, from the effects of
other scheduled transactions
It also means that changes to the database are not revealed to
users until the transaction is committed
-Transactions should not interfere with each other (these can
be effected through locking which will be discussed later)
- If transactions are isolated from one another it means that
concurrent transactions (i.e. several transactions being
executed at a given point in time) all affect the database as
if they were presented to the DBMS in a serial fashion (i.e.
they look like they are executing one after the other)
• Isolation requirement — if between steps 3 and
6, another transaction T2 is allowed to access the
partially updated database, it will see an
inconsistent database (the sum A + B will be less
than it should be).
T1 T2
1. read(A)
2. A := A – 50
3. write(A)
read(A), read(B), print(A+B)
4. read(B)
5. B := B + 50
6. write(B
• Isolation can be ensured trivially by running
transactions serially
– that is, one after the other.
DURABILITY PROPERTY
-Any changes resulting from a transaction are
permanent
-If a transaction changes the database and is
committed, the changes must never be lost
because of subsequent failure
• DBMS uses the log to ensure durability
• If the system crashed before the changes
made by a completed transaction are written
to disk, the log is used to remember and
restore these changes when the system is
restarted
• Again, this is handled by the recovery manager
DBMSs provide two services to ensure that
transactions obey the ACID properties:
Recovery transparency- means that the DBMS
automatically restores a database to a
consistent state after a failure e.g. If a
communication failure occurs during an ATM
transaction, the effects of the transaction are
automatically removed from the database; on
the other hand if the DBMS crashes 3 seconds
after an ATM transaction completes, the
details of the transaction remain permanent
Concurrency Transparency- means that users
perceive the database as a single user system
even though there may be many simultaneous
users e.g. Even though many users may try to
reserve for a flight using a reservation
transaction, the DBMS ensures that users do
not overwrite each other’s work
NON-ACID TRANSACTIONS
• There are application domains where ACID properties are not
necessarily desired or, most likely, not always possible.
• This is the case of so-called long-duration transactions
– Suppose that a transaction takes a lot of time
– In this case it is unlikely that isolation can/should be guaranteed
• E.g. Consider a transaction of booking a hotel and a flight
• Without Isolation, Atomicity may be compromised
• Consistency and Durability should be preserved
----------------- t0 -----------------
Read X t1 -----------------
---------------- t2 Read X
Update X t3 -----------------
t4 Update X
----------------- t5 -----------------
----------------- t0 -----------------
----------------- t1 Update X
Read X t2 -----------------
----------------- t3 Rollback
----------------- t4 -----------------
------------- t0 -------------
JACK INTIATES HIS TRANSACTION, THE PROGRAM PLACES A READ LOCK ON HIS RECORD SINCE
HE IS READING THE RECORD TO CHECK THE ACCOUNT BALANCE. WHEN HE REQUESTS A
WITHDRAWAL THE PROGRAM ATTEMPTS TO PLACE A WRITE LOCK. HOWEVER JILL HAD
ALREADY PLACED A SHARED LOCK ON THE RECORD. AS A RESULT HIS REQUEST IS DENIED
SINCE IF A RECORD HAS A READ LOCK ANOTHER USER CANNOT OBTAIN A WRITE LOCK
LOCKING LEVEL (GRANULARITY)
This is the extent of the database resource that
is included with each lock; locks are
implemented at the following levels:
Database- entire database is locked and becomes unavailable to other users
Table- entire table containing a requested record is locked; this level is appropriate
mainly for bulk updates such giving all employees a 5% raise
Block or page- the physical storage block(or page) containing a requested record is
locked
Record- only the requested record or row is locked; all other records are available to
other users
Field- only the particular field or column in a requested record is locked; this level is
appropriate when the updates affect only one or two fields in a records e.g you want
to update price column
PROBLEMS ASSOCIATED WITH
LOCKING
Last End of
checkpoint Log
Time
Analysis
Log
Redo pass
pass
Undo
pass
Remote Backup Systems
• Remote backup systems provide high availability by allowing transaction
processing to continue even if the primary site is destroyed.
• Detection of failure: Backup site must detect when primary site has failed
– to distinguish primary site failure from link failure maintain several
communication links between the primary and the remote backup.
– Heart-beat messages
• Transfer of control:
– To take over control backup site first perform recovery using its copy of the
database and all the log records it has received from the primary.
• Thus, completed transactions are redone and incomplete transactions
are rolled back.
– When the backup site takes over processing it becomes the new primary
– To transfer control back to old primary when it recovers, old primary must
receive redo logs from the old backup and apply all updates locally.