Topic 5 - Database Transaction Management
Topic 5 - Database Transaction Management
DATABASE DESIGN
TOPIC 5
DATABASE TRANSACTION MANAGEMENT
Prepared by:
Harzulaili Bt Saleh
COURSE LEARNING OUTCOME
Upon Completion Of This Course, Students Should Be Able To:
CLO 1 :
Apply Fundamental Of Database Management System (DBMS), Relational Data
Model And Normalization Concepts In Database Development Process. ( C3, PLO 2 )
CLO 2 :
3
5.1.1 TRANSACTION SYSTEM
TRANSACTION?
• an action, or series of actions that are being performed by a single user
or application program, which reads or updates the contents of the
database
• can be defined as a logical unit of work on the database.
• may be an entire program, a piece of a program or a single command
(like the SQL commands such as INSERT or UPDATE) and
• may engage in any number of operations on the database
• in the database context, the execution of an application program can be
thought of as one or more transactions with non-database processing
taking place in between.
4
5.1.1 TRANSACTION SYSTEM
Example of TRANSACTION
Open_Acc (Ahmad)
OldBal = Ahmad.bal
Ahmad.bal = NewBal
CloseAccount(Ahmad)
5
6
5.1.2 CATEGORIES OF TRANSACTION PROCESSING SYSTEM
7
5.1.2 CATEGORIES OF TRANSACTION PROCESSING SYSTEM
a) Batch transaction processing system
o transactions are accumulated over a period of time and processed as
a single unit, or batch
o Batch processing is a method of processing data in which data is
collected at the time of the event.
o e.g :
i. a store may update its sales records every day after the store closes.
ii. a payroll system may process all the time cards every two weeks to determine
employee earnings and produce paychecks.
iii. An example of batch processing is the way that credit card companies process
billing. The customer does not receive a bill for each separate credit card
purchase but one monthly bill for all of that monthly purchases. The bill is
created through batch processing, where all of the data are collected and held
until the bill is processed as a batch at the end of the billing cycle.
8
5.1.2 CATEGORIES OF TRANSACTION PROCESSING SYSTEM
9
5.1.2 CATEGORIES of TRANSACTION PROCESSING SYSTEM
b) On-line transaction processing system (OLTP)
• a class of software programs capable of supporting transaction-
oriented applications on the Internet
• are used for order entry, financial transactions, customer relationship
management and retail sales
• E.g: ATM
• Such systems have a large number of users who conduct short
transactions
• All transactions are processed immediately, without delay.
10
5.1.2 CATEGORIES of TRANSACTION PROCESSING SYSTEM
11
5.1.2 CATEGORIES OF TRANSACTION PROCESSING SYSTEM
c) Real time transaction processing system
• transactions are processed immediately as they occur without any
delay to accumulate transactions
• the records in the system always reflect the current status
• e.g: airline ticket reservations
• When you book a ticket and select a seat, that booking is made
right away, and nobody else can get that same seat even a
second later.
• Any changes you make to your reservation are also updated in
real time.
12
5.1.2 CATEGORIES OF TRANSACTION PROCESSING SYSTEM
c) Real time transaction processing system
13
5.1.3 PROPERTIES OF DATABASE TRANSACTION
14
5.1.3 PROPERTIES OF DATABASE TRANSACTION
• ATOMICITY
All transaction must remain whole. That is, it must completely succeed
or completely fail.
A transaction is an atomic unit. Hence, all the instructions within a
transaction will successfully execute, or none of them will execute.
E.g: The following transaction transfers 20 dollars from Alice’s bank
account to Bob’s bank account. If any of the instructions fail, the entire
transaction should abort and rollback.
15
• CONSISTENCY
A database is initially in a consistent state, and it should remain
consistent after every transaction.
For example:
1. in an application that transfers funds from one account to
another, the consistency property ensures that the total value of
funds in both the accounts is the same at the start and end of each
transaction.
2. Suppose that the transaction in the previous example fails after
Write(A_b) and the transaction is not rolled back; then, the
database will be inconsistent as the sum of Alice and Bob’s money,
after the transaction, will not be equal to the amount of money
they had before the transaction.
16
5.1.3 PROPERTIES OF DATABASE TRANSACTION
• ISOLATION
Each transaction should carry out its work independently of any other
transaction that might occur at the same time.
If the multiple transactions are running concurrently, they should not be
affected by each other. The result should be the same as the result
obtained if the transactions were running sequentially.
For example:
1. in an application that transfers funds from one account to another, the
isolation property ensures that another transaction sees the transferred
funds in one account or the other, but not in both, nor in neither.
2. Suppose B_bal is initially 100. If a context switch occurs after B_bal *= 20,
then the changes should only be visible to T2 once T1 commits. This ensures
consistency in the data and prevents incorrect results.
17
• DURABILITY
Changes that have been committed to the database should remain
even in the case of software and system failure
For Example:
1. In An Application That Transfers Funds From One Account To
Another. The Durability Property Ensures That The Changes Made To
Each Account Will Not Be Reversed.
2. For instance, if Bob’s account contains $120, this information
should not disappear upon system or software failure.
18
19
5.1.4 PERFORM TRANSACTION USING SQL STATEMENT
20
5.1.5 USE START TRANSACTION & COMMIT
21
5.1.5 USE START TRANSACTION & COMMIT
22
5.1.5 USE START TRANSACTION & COMMIT
EXAMPLE:
START TRANSACTION
UPDATE customers
SET ContactName=‘Jenn’
WHERE CustomerId = ‘XYZ’;
COMMIT TRANSACTION
24
5.1.5 USE START TRANSACTION & COMMIT
EXAMPLE:
START TRANSACTION
UPDATE customers
SET ContactName=‘David’
WHERE CustomerId = ‘XYZ’;
ROLLBACK TRANSACTION
26
CONCURRENCY CONTROL
27
5.1.6 PURPOSE OF CONCURRENCY CONTROL
• Purpose :
• to ensure that concurrent transactions do not
interfere with each other's operation.
• to ensure that several users trying to update the same
data do so in a controlled manner so that the result of
the updates is correct.
• Example:
Several reservation clerks try to assign a hotel room; the
DBMS should ensure that only one clerk could access each
hotel room at a time for assignment to a customer.
28
5.1.6 PURPOSE OF CONCURRENCY CONTROL
29
5.1.7 PROBLEMS OF CONCURRENCY CONTROL
a. Lost update
b. Uncommitted data
c. Inconsistent retrieval
30
A. LOST UPDATE
• Occur when two transactions, accessing the same data items,
have their operations interleaved in such a way, that one
transaction will access the data before the other has applied
any updates.
31
A. LOST UPDATE
33
B. UNCOMMITTED DATA
• Example:
– T4 updates balx to RM200 but it aborts, so balx should be back at
original value of RM100
– T3 has read new value of balx (RM200) and uses value as basis
of RM10 reduction, giving a new balance of RM190, instead
of RM90
35
B. UNCOMMITTED DATA
36
C. INCONSISTENT RETRIEVAL
• Occurs when a transaction calculates some summary
function over a set of data while the other transactions
are updating the data
Some data may be read after they are changed and some before they are
changed yielding inconsistent results
• aka unrepeatable read
• Example:
– T6 is totaling balances of account x (RM100), account y
(RM50), and account z (RM25).
– Meantime, T5 has transferred RM10 from balx to balz, soT6
now has wrong result (RM10 too high).
37
C. INCONSISTENT RETRIEVAL
39
5.1.8 CONCURRENCY CONTROL WITH LOCKING METHOD
•Lock
Guarantees exclusive use of a data item to a current
transaction
T2 does not have access to a data item that is
currently being used by T1
Transaction acquires a lock prior to data access;
the lock is released when the transaction is
complete
Required to prevent another transaction from
reading inconsistent data
•Lock manager
Responsible for assigning and policing the locks used
by the transactions
40
5.1.8 CONCURRENCY CONTROL WITH LOCKING METHOD
a. Lock Granularity
b. Lock Types
c. Two Phase Locking to ensure serializability
d. Deadlocks
41
A. LOCK GRANULARITY
• Locks may be placed at various levels (known as lock granularity)
• Some database products (Sybase, IBM’s DB2) support
multiple levels with automatic lock escalation
• Typical lock level:
• Database - entire database is locked
• File - entire database file is locked
• Table - entire table is locked
• Block or page - block/page within database file is locked
• Row - row in a table is lock
• Column - some columns within a row in the table are locked
42
B. LOCK TYPES
Three types of locks (lock modes) :
i. read lock (rl) – also called shared lock
ii. write lock (wl) – also called exclusive lock
iii. binary lock - a lock on a data item can be in two states; it is
either locked or unlocked.
Allowing more than one transaction to write on the same data item
would lead the database into an inconsistent state.
Read locks are shared because no data value is being changed.
43
C. TWO-PHASE LOCKING (2PL) TO
ENSURE SERIALIZABILITY
• Defines how transactions acquire and release locks
• Guarantees serializability, but it does not prevent deadlocks
Growing phase, in which a transaction acquires all the
required locks without unlocking any data
Shrinking phase, in which a transaction releases all locks and
cannot obtain any new lock
44
C. TWO-PHASE LOCKING (2PL) TO ENSURE
SERIALIZABILITY
• Governed by the following rules:
o Two transactions cannot have conflicting locks
o No unlock operation can precede a lock operation in the
same transaction
o No data are affected until all locks are obtained—that is,
until the transaction is in its locked point
45
C. TWO-PHASE LOCKING (2PL) TO ENSURE
SERIALIZABILITY
The lock point is the moment when transitioning from the
growing phase to the shrinking phase
46
C. TWO-PHASE LOCKING (2PL) TO ENSURE
SERIALIZABILITY
47
C. TWO-PHASE LOCKING (2PL) TO ENSURE
SERIALIZABILITY
• Properties of the 2PL protocol
Generates conflict-serializable schedules
48
PREVENTING LOST UPDATE USING 2PL
49
PREVENTING UNCOMMITTED DATA USING 2PL
50
PREVENTING INCONSISTENT RETRIEVAL USING 2PL
51
D. DEADLOCKS
52
For example: In the student table, transaction T1 holds a lock on some rows
and needs to update some rows in the grade table. Simultaneously, transaction
T2 holds locks on some rows in the grade table and needs to update the rows
in the Student table held by Transaction T1.
53
EXAMPLE :
54
• T1 needs data items X and Y; T2 needs Y and X
• Each gets the its first piece of data but then waits to get the second
(which the other transaction is already holding)
Control through:
Prevention
Detection
Avoidance
55
• To break deadlock: abort one or more of transactions
– Deadlock transparent to user
– DBMS restarts transaction(s)
56
5.1.9 DATABASE RECOVERY MANAGEMENT
57
5.1.9 DATABASE RECOVERY MANAGEMENT
a) Database recovery
b) Transaction recovery
c) Database back-up
58
A. DATABASE RECOVERY TECHNIQUE
59
A. DATABASE RECOVERY TECHNIQUE
60
A. DATABASE RECOVERY TECHNIQUE
61
B. TRANSACTION RECOVERY
Some approaches for recovery from non-catastrophic
(transaction) failures can be classified as either:
62
B. TRANSACTION RECOVERY
Deferred update
63
B. TRANSACTION RECOVERY
64
B. TRANSACTION RECOVERY
Immediate update
65
B. TRANSACTION RECOVERY
Immediate update
PROCEDURE of RIU:
66
C. DATABASE BACKUP
• A Backup, Or The Process Of Backing Up, Refers To The
Copying And Archiving Of Computer Data So It May Be Used
To Restore The Original After A Data Loss Event.
67
C. DATABASE BACK-UP
68
C. DATABASE BACKUP
• It Is Assumed That You Have Phpmyadmin Installed Since A Lot Of Web Service Providers Use It.
• To Backup Your Mysql Database Using Phpmyadmin Just Follow A Couple Of Steps:
• Open Phpmyadmin.
• Select Your Database By Clicking The Database Name In The List On The Left Of The Screen.
• Click The Export Link. This Should Bring Up A New Screen That Says View Dump Of Database (Or
Something Similar).
• In The Export Area, Click The Select All Link To Choose All Of The Tables In Your Database.
• Click On The Save As File Option And The Corresponding Compression Option And Then Click The 'Go'
Button. A Dialog Box Should Appear Prompting You To Save The File Locally.
69
70