Professional Documents
Culture Documents
Chapter04baru Transaction
Chapter04baru Transaction
Chapter 4
Transaction
Introduction
An application might face problems such as:
a power failure
a hardware crash
some accidently unplugs a cable, etc.
Salmi: 1424.27
Transaction Issues
How do we know that both operations are in the
same transaction?
And, how does the computer know that both
operation must be completed together?
How do we tell the computer system which
operations belong to a transaction?
Transaction Issues ..cont.
Business rules define what operations should be in a
transaction.
The application developer responsible to tell the
computer system which operation belong in a
transaction.
By marking the start and the end of transaction in your
code.
When the computer sees the start marking, it first writes
all the changes to a log file. When it reaches the end
mark, it makes the actual changes to the data in the
correspond tables.
Defining Transaction
The computer needs to be told which changes must
be grouped into a transaction.
Turn on transaction processing.
7
SQL Transaction Code
8
START TRANSACTION;
SELECT
UPDATE
SAVEPOINT StartOptional;
UPDATE
UPDATE
If error THEN
ROLLBACK TO SAVEPOINT StartOptional;
END IF
COMMIT;
Example:
START TRANSACTION;
SELECT * FROM EMPLOYEE;
UPDATE EMPLOYEE SET SALARY = SALARY * 1.05
UPDATE TOTAL SET SAL =
SAVEPOINT StartOptional;
INSERT INTO PAYSLIP VALUES (SELECT EMPID, , SALARY * 1.05)
If error THEN
ROLLBACK TO SAVEPOINT StartOptional;
END IF
COMMIT;
Concurrent Access
Concurrent Access Two processes
Multiple users or processes Receive payment ($200)
changing the same data at Place new order ($150)
the same time.
Initial balance $800
Final data will be wrong!
Result should be $800 -200
Force sequential + 150 = $750
Locking Interference result is either
Delayed, batch updates $600 or $950
12
Pessimistic Locks: Serialization
13
15
Transaction to Transfer Money
16
2) Lock Data B
4) Wait for Data A
Deadlock Sequence
18
Solution 1: Random Wait Time
When a process receives a message that is must
wait for a resource:
the process wait for a random length of time,
try again,
Process3 Lock
Process5 Wait