INF20010 LEC 05 Trans Concurrency VBTrans-1
INF20010 LEC 05 Trans Concurrency VBTrans-1
Database Systems
Lecture 5
Transactions
Commit
Rollback
ACID properties
Concurrency Control
Select … For Update
VB Transaction
2
Transactions & concurrency
This semester you have built some databases
Event
EventID Integer Primary Key,
EventName Varchar2(100),
TicketsAvailable Integer );
5
Transactions & concurrency
The user wishes to purchase 6 tickets
The user types the qty required and presses
the Buy button
6
Transactions & concurrency
The SQL statements might look something like this:
UPDATE EventTable
SET TicketsAvailable = TicketsAvailableTextBox.Text - TicketsRequiredTextBox.Text
WHERE eventid = EventTextBox.Text;
COMMIT;
The syntax above may need slight changes to work correctly. This is an overview of the process.
7
Transactions & concurrency
After the sale of the tickets, the Sales Form is updated to reflect the
tickets now available
100 at beginning – 6 sales = 94 ticket available
8
Transactions & concurrency
You could imagine this process continuing.
Users continue to purchase tickets
It all seems OK.
9
Transactions & concurrency
What happens in a multi-user environment?
BOTH users view a Sales Form on their computer
BOTH users search for the same Event
BOTH users see the TicketsAvailable value for the event
USER 1 USER 2
10
Transactions & concurrency
User1 clicks the Buy button. The SQL statements are executed
UPDATE EventTable
SET ticketsavailable = TicketsAvailableTextBox.Text - TicketsRequiredTextBox.Text;
97
11
Transactions & concurrency
User2 clicks the Buy button. The SQL statements are executed
UPDATE EventTable
SET ticketsavailable = TicketsAvailableTextBox.Text - TicketsRequiredTextBox.Text;
92
12
Transactions & concurrency
Something has gone wrong.
92
13
Lost Update problem
This is referred to as a Lost Update problem.
This is because the system has not dealt with the possibility
of multiple users updating the same data rows at the same time.
Shared Locks
While a row has a Shared Lock placed on it
The row can be read by other transactions
The row cannot be updated by other transactions
Exclusive Locks
While a row has a Exclusive Lock placed on it
The row cannot be read by other transactions
The row cannot be updated by other transactions
16
Lock Granularity
Lock Granularity
This refers to how large a section of the database you need to lock.
USER 2
Required Tickets : 5
Sees that only 2 TicketsAvailable
Exit Sales Form. User misses out on tickets for the event
19
Uncommitted Read / Dirty Read problem
But wait…
User 1's credit authorization fails. The credit limit is exceeded!
USER 1
Rollback the transaction
All updates created by the transaction are reveresed
TicketsAvailable value 10
USER 2
Has no knowledge of this
User misses out on tickets for the event
Users 2 had read data that was not committed
This is known as an Uncommitted Read or Dirty Read
This is dangerous. Unstable data is being used.
The database is not in a Consistent State yet
20
Transaction Isolation
There are various techniques used to stop transactions
interfering with each other
Consistency
A transaction takes database from one consistent state to another.
Implemented by database integrity constraints.
Isolation
Transactions are isolated from one another. A transaction that
Durability
Ensures the results of a committed transaction cannot be lost, even
Transaction 1 End
Begin
Transaction 2 End
Begin
Transaction 3 End
23
Isolation and Serializability
Two transaction can be executed in perfect isolation if they run serially.
Each transaction is consistent and isolated from all others
Isolation is guaranteed
the schedule is guaranteed to be correct for all applications.
Two transactions can run concurrently (at the same time) so long as the
effect is the same as executing true serial execution.
3. --Update qoh
2 UPDATE PRODUCT SET qty = vQty
WHERE ProdId = vProdId ;
4. --Insert order
3 INSERT INTO SALES VALUES(SSeq.Nextval,vProdId,vOrderQty);
4
26
SERIALIZABLE Example
Imagine two customers both purchasing products at the same time
Results in two transactions are run concurrently
TA for the purchase of product P001
TB for the purchase of product P001
1 2 3 4 1 2 3 4 Serial (1)
1 2 3 1 4 2 3 4 Serializable (1)
1 2 3 1 2 4 3 4 Serializable (2)
1 2 3 4 1 2 3 4 Serial (2)
27
Testing Schedules for SERIALIZABILITY
Transactions which run under SERIALIZABLE isolation level
leave the database in the same consistent state as a serial schedule
(no anomalies).
For example,
1 2 3 1 2 4 3 4
1 2 1 3 4 2 3 4 Not Serializable
qty
R 2 R W 4 2 W 4 TA TB
qty
29
Isolation Levels
Serializability provides a conservative definition of correctness
For a particular application there might be many
acceptable non-serializable schedules
Requiring serializability might degrade performance
5
SELECT Balance INTO vBalance
Transaction B:
FROM bankcust WHERE custid = 1; bankcust
Update
6
Set Balance = 0
7 Where custid = 1;
vNewBalance = vBalance +
Trans B is executed BEFORE line 8 is executed
pamt;
8
9 UPDATE bankcust
10 SET Balance = vNewBalance WHERE
vbalance custid
is wrong!
11 = 1; newBalance is wrong!
12 End;
47
Lost Update problem
Transaction A
Transaction
Create Procedure Upd(pamt number) AsB set
1 the balance to 0.
2 vBalance NUMBER;
3
vNewBalance NUMBER; Yet Transaction A
BEGIN will overwrite that
4 value.
SELECT Balance INTO vBalance
5
FROM bankcust WHERE custid =A 1;Lost Update
6 will occur
7 vNewBalance = vBalance + pamt;
8
9 UPDATE bankcust
10 SET Balance = vNewBalance WHERE custid
11 = 1;
12 End;
48
SELECT... FOR UPDATE solution
Transaction A
In this better
1 Create Procedure Upd(pamt number) As
alternative,
vBalance NUMBER; Transaction A now
2
vNewBalance NUMBER; has a FOR UPDATE
3 BEGIN clause in the
4 Select statement
SELECT Balance INTO vBalance
5 FROM bankcust WHERE custid = 1 FOR
6 UPDATE;
7
8 vNewBalance = vBalance + pamt;
9
UPDATE bankcust
10
SET Balance = vNewBalance WHERE custid =
11 1;
12 End;
49
SELECT... FOR UPDATE solution
Transaction A
1 Create Procedure Upd(pamt number) As
vBalance NUMBER;
2
vNewBalance NUMBER;
3 BEGIN
4 SELECT Balance INTO vBalance
5 FROM bankcust WHERE custid = 1 FOR
6 UPDATE;
7
The row of+
vNewBalance = vBalance cust 1 is locked
pamt;
8
9
UPDATE bankcust
10
SET Balance = vNewBalance WHERE custid =
11 1;
12 End;
50
Lost Update problem
Transaction B Transaction B
1 Create Procedure Upd(pamt number)
attemptsAsto execute
vBalance NUMBER; line 6
2
vNewBalance NUMBER;
3 This is impossible.
BEGIN Cust 1 has been
4 SELECT Balance INTO vBalance locked.
5 FROM bankcust WHERE custid = 1 FOR
6 UPDATE;
7
8 vNewBalance = vBalance + pamt; Transaction B
must wait...
9
UPDATE bankcust
10 Until Transaction A
SET Balance = vNewBalance WHERE custid =
releases the lock
11 1; (i.e. commit or rollback)
12 End;
51
Oracle Select ... For Update
Transaction C Transaction D
... ...
vqty PRODUCT.qty%TYPE; vqty PRODUCT.qty%TYPE;
BEGIN BEGIN
INSERT INTO SALES ...; INSERT INTO SALES ...;
SELECT QTY INTO vqty FROM PRODUCT
WHERE PRODID = '1' FOR UPDATE;
Time
Row is locked Transaction D is paused.
vqty := vqty – pPurchaesQty; It must wait for locks from
Transaction 1 to be released
UPDATE PRODUCT
SET QTY = vqty
WHERE PRODID = ’1’ ;
COMMIT; Row locks released
SELECT QTY INTO vqty FROM PRODUCT
WHERE PRODID = '1' FOR UPDATE;
END;
vqty := vqty – pPurchaesQty;
UPDATE PRODUCT
SET QTY = vqty
WHERE PRODID = ’1’ ;
COMMIT;
END;
52
Consider this procedure...
We want to transfer money from one customer to
another
1
Create Procedure TRANSFER_CASH (pCust1
2 NUMBER,
3 pCust2 NUMBER,
4 pAmt NUMBER) AS
5 vBal1 ACCOUNT.balance%TYPE;
6 vBal2 ACCOUNT.balance%TYPE;
BEGIN
7 SELECT balance INTO vBal1 FROM ACCOUNT
8 WHERE custid = pCust1;
SELECT balance INTO vBal2 FROM ACCOUNT
WHERE
What can we custid = the
assume about pCust2;
values held in vBal1 and vBal2 at this point in time
... especially in a multi-user environment?
ALWAYS assume that the values in vBal1 and vBal2 are out of date.
Solution: Use Select For Update.
53
Consider this procedure...
We want to transfer money from one
customer to another
9
10 vBal1 = vBal1 – 20;
11 vBal2 = vBal2 + 20;
12 UPDATE ACCOUNT
13 SET balance = vBal1 WHERE custid =
14 pCust1;
15 UPDATE ACCOUNT
SET balance = vBal2 WHERE custid =
pCust2;
What can we assume about the values held in vBal1 and vBal2 at this point in time
especially in a multi-user environment?
END;
ALWAYS assume that the values in vBal1 and vBal2 are out of date.
Solution: Use Select For Update.
55
Improved code with Select... For Update
vBal1 ACCOUNT.balance%TYPE;
vBal2 ACCOUNT.balance%TYPE;
BEGIN
SELECT balance INTO vBal1 FROM ACCOUNT
WHERE custid = pCust1 FOR UPDATE;
SELECT balance INTO vBal2 FROM ACCOUNT
WHERE custid = pCust2 FOR UPDATE;
-- At this point, no other transaction can update
these customer balances
vBal1 = vBal1 – 20;
vBal2 = vBal2 + 20;
UPDATE ACCOUNT
SET balance = vBal1 WHERE custid =
pCust1;
UPDATE ACCOUNT
SET balance = vBal2 WHERE custid =
pCust2 ;
COMMIT; -- Releases locks
END;
56
VB GUI
This is the GUI for the Demo application
57
Reset Table - Transaction Code
This code shows the Reset Table Transaction in the Demo application
58
Part 1…Create a Transaction Object
Create & Open a Connection
Any
Error messages raised by one part of the transaction must be
handled by the code which begins and ends the transaction