2. Lecturers
Vanderbijlpark Campus (VC)
Lecturer: Dr. Jaco Pretorius
Position: Lecturer and Subject Coordinator
Email: Jaco.Pretorius@nwu.ac.za
Telephone +27 16 910 4985
Building and Office: Building 8 - G35
Consultation Hours: Please see availability, per appointment
Potchefstroom Campus (PC)
Lecturer: Mr. Henri van Rensburg
Position: Lecturer
Email: Henri.vanRensburg@nwu.ac.za
Telephone +27 18 299 4275
Building and Office: Building G3 - G22
Consultation Hours: Please see availability, per appointment
3. Contact & Consultation
• Emails: any time during the week
• In person: after class /
during cosultation time (in office) /
per appointment
• Online: per appointment
• Module support: Mr. Given Mnisi
(givenmnisi6@gmail.com) and
•
Mr. Lesetja Mojapelo
(lesetjamojapelo@yahoo.com)
4. CMPG321
• Handboek / Textbook
• Database Systems, 13th Ed
- Design, Implementation and Management
• Author: Coronel, C., Morris, S.
• Any additional resources / material will be announced on
eFundi
5. CMPG321
• eFundi (http://efundi.nwu.ac.za)
• Elektroniese leeromgewing / Electronic learning environment
• Kennisgewings / Announcements
• Opdragte ontvang en indien / Assignments receive and submit
• Hulpbronne / Resources
• Studiegids as struktuur /
Study guide as structure
* Maak seker dat jy eFundi gereeld besoek! /
* Ensure you visit eFundi regularly!
7. General Admin
• Resources on eFundi, general & additional
• Classes:
• Generally one contact class per week
• Please feel free to join the online class on Wednesdays /
watch the class recording afterwards
• If you don’t have access to eFundi, stay after class
• Class representative – ??? (email?)
• SQL Environment? Oracle, MySQL
8. CMPG321 –
ADVANCED DATABASES
Study Unit 1
(DS - Chapter 10)
Fakulteit Natuur- en Landbouwetenskappe
Faculty of Natural- and Agricultural Sciences
9. SU1 - Outcomes
Transaction management and concurrency control
• Discuss database transactions and their properties;
• Determine what concurrency control is and what role it plays in
maintaining the database's integrity;
• Illustrate what locking methods are and how they work;
• Describe how stamping methods are used for concurrency control;
• Describe how optimistic methods are used for concurrency control; and
• Discuss how database recovery management is used to maintain
database integrity.
13. 13
Initial state
Update Transaction
Final state <Consistent state>
<Modifies database>
<Consistent state>
PROD_QOH = 200
PROD_QOH = PROD_QOH - 12
PROD_QOH = 188
Table: PRODUCT Field: PROD_QOH Initial value: 200
The product: Product update
14. 14
Transactions
▪ Consider the following situation.
Sam is transferring R100 from his bank account to his
friend Jim’s.
– Sam’s account should be reduced by 100.
– Jim’s account should be increased by 100.
What should happen logically?
15. 15
Transactions
▪ Consider the following situation.
Sam is transferring R100 from his bank account to his
friend Jim’s.
– Sam’s account should be reduced by 100.
– Jim’s account should be increased by 100.
16. 16
Sam’s account should be reduced by 100.
Q1. Which of the following SQL statements is
correct for the above operation? Assume Sam’s
account number is ‘123’.
A. UPDATE account
SET balance = balance – 100;
B. UPDATE account
SET balance= balance – 100
WHERE acc_no = '123';
C. UPDATE account
SET acc_no = balance + 100;
D. UPDATE account
SET balance = balance + 100
WHERE acc_no = '123';
17. 17
UPDATE
▪ Changes the value of existing data.
▪ For example, at the end of semester, change the
mark and grade from null to the actual mark and
grade.
UPDATE table
SET column = (subquery) [, column = value, ...]
[WHERE condition];
UPDATE enrolment
SET mark = 80,
grade =‘Distinction'
WHERE sno = 12345678
and ……
UPDATE enrolment
SET mark = 85
WHERE unit_code = (SELECT unit_code FROM unit WHERE
unit_name=‘Advanced Databases')
AND mark = 80;
18. 18
DELETE
• Removing data from the database
DELETE FROM table
[WHERE condition];
DELETE FROM enrolment
WHERE sno=‘12345678'
AND
unit_code= (SELECT unit_code
FROM unit
WHERE unit_name='Introduction to Databases' )
AND
semester='1'
AND
year=‘2022';
19. 19
Assume that Jim’s account number is '333'. The transfer of
money from Sam’s to Jim’s account will be written as the
following SQL transaction:
UPDATE account
SET balance= balance – 100
WHERE acc_no = '123';
UPDATE account
SET balance= balance + 100
WHERE acc_no = '333';
COMMIT;
SQL
statements
T
R
A
N
S
A
C
T
I
O
N
All statements need to be run as a single logical
unit operation.
22. 22
Transaction Properties
▪ A transaction must have the following properties:
– Atomicity
• all database operations (SQL requests) of a transaction must
be entirely completed or entirely aborted
– Consistency
• it must take the database from one consistent state to another
– Isolation
• it must not interfere with other concurrent transactions
• data used during execution of a transaction cannot be used
by a second transaction until the first one is completed
– Durability
• once completed the changes the transaction made to the data
are durable, even in the event of system failure
▪ Serializability
– Ensures that the schedule for the concurrent execution of several
transactions should yield consistent results
24. 24
Q2. According to the atomicity property, the
transaction below is complete when statement
number _____ is completed.
UPDATE account
SET balance= balance – 100
WHERE acc_no = '123';
UPDATE account
SET balance= balance + 100
WHERE acc_no = '333';
COMMIT;
A. 1
B. 2
C. 3
D. None of the above.
1
2
3
25. 25
Q3. Which transaction property is violated when a
transaction T2 (Jim checking the account balance)
is allowed to read the balance of Jim’s account
while the transaction T1 (the money transfer from
Sam’s to Jim’s) has not been completed?
A. Atomicity.
B. Isolation.
C. Consistency.
D. Durability.
26. 26
Consistency - Example
▪ Assume that the server lost its power during the execution
of the money transfer transaction, only the first statement is
completed (taking the balance from Sam’s).
▪ Consistency properties ensure that Sam’s account will be
reset to the original balance because the money has not be
transferred to Jim’s account.
▪ ? - The last consistent state is when the money transfer
transaction has not been started.
27. 27
Durability - Example
▪ Assume the server lost power after the commit statement
has been reached.
▪ The durability property ensures that the balance on both
Sam’s and Jim’s accounts reflect the completed money
transfer transaction.
31. Recap: Transaction Management
DEF: A transaction symbolizes a logical unit of work performed within a
DBMS (or similar system) that must be entirely completed or aborted.
(treated in a coherent and reliable way independent of other
transactions. a Transaction generally represents any change in a
database.)
• Follows the ACID properties.
• Atomicity, Consistency, Isolation, Durability
• Transaction boundaries
• Start
• first SQL statement is executed (eg. Oracle)
• Some systems have a BEGIN WORK type
command
• End
• COMMIT or ROLLBACK
• This week: Concurrency Management Serializability
32. 32
Transaction Properties
▪ A transaction must have the following properties:
– Atomicity
• all database operations (SQL requests) of a transaction must
be entirely completed or entirely aborted
– Consistency
• it must take the database from one consistent state to another
– Isolation
• it must not interfere with other concurrent transactions
• data used during execution of a transaction cannot be used
by a second transaction until the first one is completed
– Durability
• once completed the changes the transaction made to the data
are durable, even in the event of system failure
▪ Serializability
– Ensures that the schedule for the concurrent execution of several
transactions should yield consistent results
33. •Many of you who
studied Operating
Systems and Parallel
Processing (and similar
units) will notice
similarities with
operating system design
issues/theory.
Versioning??
34. Serial and Interleaved transactions.
T0 T1
Read(X)
X=X+1
Write(x)
Read(Y)
Y=Y*2
Write(Y)
Read(x)
X=X+2
Write(X)
T0 T1
Read(X)
Read(Y)
Y=Y*2
X=X+1
Write(x)
Write(Y)
Read(x)
X=X+2
Write(X)
Serial Interleaved (non Serial)
Time:
One of the fundamental properties of a Tx is ISOLATION
What will happen if several transaction execute concurrently?
Isolation may no longer be preserved…
35. Concurrency
Control – all
based on
Serializability
principle
Try to manage/ prevent
• Lost updates
• Uncommitteddata
• Inconsistent retrievals
40. T0 T1
Read(X)
X=X+1
Write(x)
Read(Y)
Y=Y*2
Write(Y)
Read(x)
X=X+2
Write(X)
Time:
Writing schedules
• In Transaction Management, only the following operations
are considered: Read, Write, Commit, and Abort.
• Therefore, the Serial Schedule below can be written as:
S1: r0(X); w0(X); c0; r1(Y); w1(Y); r1(X); w1(X); c1;
• Note that r means read, w means write, and c means
commit. Other operations are omitted. 0 and 1 indicate the
transaction number.
41. • If transactions cannot be interleaved, there are two possible correct
schedules: one that has all operations in T0 before all operations in
T1, and a second schedule that has all operations in T1 before all
operations in T0 (these are known as serial schedules).
Serial Schedule 1: [r0(X); w0(X); c0]; [r1(Y); w1(Y); r1(X); w1(X); c1];
Serial Schedule 2: [r1(Y); w1(Y); r1(X); w1(X); c1]; [r0(X); w0(X); c0];
• If transactions can be interleaved, there may be many possible
interleavings (nonserial schedules)
Interleaved Schedule 1:
r0(X); r1(Y); w0(X); w1(Y); r1(X); c0; w1(X); c1;
Interleaved Schedule 2:
r0(X); r1(Y); w1(Y); r1(X); w1(X); c1; w0(X); c0;
42. “A given interleaved execution of some set of transactions is said to be serializable if and only if it
produces the same result as some serial execution of those same transactions”.
Question:
T0 T1
Read(X)
X=X+1
Write(X)
Read(Y)
Y=Y*2
Write(Y)
Read(X)
X=X+2
Write(X)
Serial
T0 T1
Read(X)
Read(Y)
Y=Y*2
Write(Y)
X=X+1
Write(X)
Read(X)
X=X+2
Write(X)
Interleaved
T0 T1
Read(X)
Read(Y)
Y=Y*2
Write(Y)
Read(X)
X=X+2
X=X+1
Write(X)
Write(X)
Interleaved
Serializability: A property in which the selected order of concurrent tx operations creates the same final database state that
Would have been produced if the tx’s had been excecuted in serial fasion
1) Suppose x = 10 and y = 20 – What is answer to schedule 1, 2 and 3?
2) Which one of 2 or 3 is serializable?
(1) (3)
(2)
43. “A given interleaved execution of some set of transactions is said to be serializable if and only if it
produces the same result as some serial execution of those same transactions”.
Question:
T0 T1
Read(X)
X=X+1
Write(X)
Read(Y)
Y=Y*2
Write(Y)
Read(X)
X=X+2
Write(X)
Serial
T0 T1
Read(X)
Read(Y)
Y=Y*2
Write(Y)
X=X+1
Write(X)
Read(X)
X=X+2
Write(X)
Interleaved
This is serializable
T0 T1
Read(X)
Read(Y)
Y=Y*2
Write(Y)
Read(X)
X=X+2
X=X+1
Write(X)
Write(X)
Interleaved
This is NOT serializable
Serializability: A property in which the selected order of concurrent tx operations creates the same final database state that
Would have been produced if the tx’s had been excecuted in serial fasion
1) Suppose x = 10 and y = 20 – What is answer to schedule 1, 2 and 3?
2) Which one of 2 or 3 is serializable?
(1) (3)
(2)
44. Let’s learn to understand serializability
Assume at the start: X = 10, Y = 20 (bold and underline used for clarity)
X=10+1
Y=20*2
X=11+2
X=10+1
Y=20*2 Y=20*2
X=11+2
X=10+1 X=10+2
Finally: X=???
X = 13
Y = 40
X = 13
Y = 40
X = ???
Y = 40
45. •“A given interleaved execution of some set
of transactions is said to be serializable if
and only if it produces the same result as
some serial execution of those same
transactions”.
•For interleaved schedules, we need to
determine whether the interleaved
schedules are correct, i.e., are serializable.
• “Serializable schedules are equivalent to
a serial schedule of the same
transactions”.
46. CMPG321
• Homework assignment
• Ensure you can sign onto eFundi and see the
“CMPG321 1-1 V 2023” page
• Go through all the information & resources available on eFundi
• Get the textbook
• Read through Chapter 10 -
Transactions and Concurrency Control
• Next week – Locks??
• Next Class: Monday 31 July @ 11:00