Top Banner
LeongHW, SOC, NUS (UIT2201:3 Database) Page 1 Copyright © 2007-9 by Leong Hon Wai Database – Info Storage and Retrieval Aim: Understand basics of Info storage and Retrieval; Database Organization; DBMS, Query and Query Processing; Work some simple exercises; Concurrency Issues (in Database) Readings: [SG] --- Ch 13.3 Optional: Some experiences with MySQL, Access
25

Database – Info Storage and Retrieval

Feb 23, 2016

Download

Documents

Paiva

Database – Info Storage and Retrieval. Aim: Understand basics of Info storage and Retrieval; Database Organization; DBMS, Query and Query Processing; Work some simple exercises; Concurrency Issues (in Database) Readings: [SG] --- Ch 13.3 Optional: - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 1

Copyright © 2007-9 by Leong Hon Wai

Database – Info Storage and Retrieval

Aim: Understand basics of Info storage and Retrieval; Database Organization; DBMS, Query and Query Processing; Work some simple exercises; Concurrency Issues (in Database)

Readings: [SG] --- Ch 13.3

Optional: Some experiences with MySQL, Access

Page 2: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 2

Copyright © 2007-9 by Leong Hon Wai

Coffee Awards in UIT2201

Coffee awards goes to interesting, creative

things done in tutorials, good attempt on

A-problems, interesting suggestions

or comments in class

Claim them anytime Just email me To get a coffee and chat

Page 3: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 3

Copyright © 2007-9 by Leong Hon Wai

Concurrency Issues

Concurrency When 2 processes access S at the same time! Can cause all kinds of problems Important issue in all areas

Illustrate with concurrency issue in database Readings for Concurrency Issues

Record Locking – Wikipedia http://en.wikipedia.org/wiki/Record_locking

Read [SG3] Section 6.4.1 (pp. 268--272) Efficient Allocation and Safe Use of Resources

Page 4: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 4

Copyright © 2007-9 by Leong Hon Wai

Concurrency Issue: Example

Bank account info:

Withdraw-P (amt);begin check balance; bal bal - amt;end;

Deposit-P (amt);begin check balance; bal bal + amt;end;

Deposit and Withdrawal Processes

BANK-ACCOUNT-DB

Account # Name Balance Other Info…

2201-1022 Albert Bank 5000 …

Page 5: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 5

Copyright © 2007-9 by Leong Hon Wai

Normal Operations (1/2)

Bank-Account-DB2201-10225000

Withdraw-P(1000);

Deposit-P(2000);

TW

TD

Time Withdraw-P(1000) Deposit(2000) Balance

TW Check balance [5000] 5000

TW+1 Bal Bal – 1000 4000

... . . . . . . 4000

TD Check balance [4000] 4000

TD+1 Bal Bal + 2000 6000

If (TW < TD), then

Correct balance

Page 6: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 6

Copyright © 2007-9 by Leong Hon Wai

Normal Operations (2/2)

Bank-Account-DB2201-10225000

Withdraw-P(1000);

Deposit-P(2000);

TW

TD

Time Withdraw-P(1000) Deposit(2000) Balance

TD Check balance [5000] 5000

TD+1 Bal Bal + 2000 7000

... . . . . . . 7000

TW Check balance [7000] 7000

TW+1 Bal Bal – 1000 6000

If (TW > TD), then

Correct balance

Page 7: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 7

Copyright © 2007-9 by Leong Hon Wai

Concurrency Issue (1/4)

But… What if two processes accesses the same database record at the same time?

Which process get access first? Does it matter?

Page 8: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 8

Copyright © 2007-9 by Leong Hon Wai

Concurrency Problem (2/4)

Bank-Account-DB2201-10225000

Withdraw-P(1000);

Deposit-P(2000);

TW

TD

Time Withdraw-P(1000) Deposit(2000) Balance

TD Check balance [5000] 5000

TD+1 Check balance [5000] 5000

TD+2 Bal Bal + 2000 7000

TD+3 Bal Bal – 1000 4000

If (TW = TD), and Deposit-process “got in” first

Wrong balance

Page 9: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 9

Copyright © 2007-9 by Leong Hon Wai

Concurrency Problem (2/3)

Bank-Account-DB2201-10225000

Withdraw-P(1000);

Deposit-P(2000);

TW

TD

Time Withdraw-P(1000) Deposit(2000) Balance

TW Check balance [5000] 5000

TW+1 Check balance [5000] 5000

TW+2 Bal Bal – 1000 4000

TW+3 Bal Bal + 2000 7000

If (TW = TD), and Withdraw-process “got in” first

Wrong balance

Page 10: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 10

Copyright © 2007-9 by Leong Hon Wai

Concurrency Problem (3/3)

Operations of the two processes are interleaved Withdraw-Process and Deposit-Process

“interfere” with each other

Wrong balance for both cases Since one of the operations is over-written

Page 11: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 11

Copyright © 2007-9 by Leong Hon Wai

Concurrency Solution: Lock operation

IDEA: If one process P is changing balance, make sure that other processes do not access the same balance until P is done

Solution: The process that “get-in” first, locks up the record.

This makes sure other processes will not be to access the same record. Unlock the record after update is done.

Page 12: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 12

Copyright © 2007-9 by Leong Hon Wai

Concurrency Solution: (2/4)

Withdraw-P (amt);begin Get & lock record; bal bal - amt; unlock record;end;

Deposit-P (amt);begin Get & lock record; bal bal + amt; unlock record;end;

Deposit and Withdrawal Processes

Bank-Account-DB2201-10225000

Page 13: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 13

Copyright © 2007-9 by Leong Hon Wai

Concurrency Solution: (3/4)

Bank-Account-DB2201-10225000

Withdraw-P(1000);

Deposit-P(2000);

TW

TD

Time Withdraw-P(1000) Deposit(2000) Balance

TW Get & Lock record; 5000

TW+1 Get...; [blocked] 5000

TW+2 Bal Bal – 1000;Unlock record;

4000

TW+3 Get & Lock record; 4000

TW+4 Bal Bal + 2000;Unlock record;

6000

If (TW = TD), and Withdraw-process “got in” first

Page 14: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 14

Copyright © 2007-9 by Leong Hon Wai

Concurrency Solution: (4/4)

Bank-Account-DB2201-10225000

Withdraw-P(1000);

Deposit-P(2000);

TW

TD

Time Withdraw-P(1000) Deposit(2000) Balance

TW Get & Lock record; 5000

TW+1 Get...; [blocked] 5000

TW+2 Bal Bal + 2000;Unlock record;

7000

TW+3 Get & Lock record; 7000

TW+4 Bal Bal – 1000;Unlock record;

6000

If (TW = TD), and Deposit-process “got in” first

Page 15: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 15

Copyright © 2007-9 by Leong Hon Wai

Case Study:The ATM Machine

Page 16: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 16

Copyright © 2007-9 by Leong Hon Wai

Simple ATM Scenario (1 of 2)

Bank User

ATMMachine

BankHQ

Inserts ATM card

Prompt for PIN #

Enter the PIN #

Send encoded Acct #, PIN #

Send encoded Verified Msg

Display Options

Choose (Withdraw $100)

[Verifies A/C # & Pin #]

[Reads card]

[encode]

network

Page 17: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 17

Copyright © 2007-9 by Leong Hon Wai

Simple ATM Scenario (2 of 2)

Bank User

ATMMachine

BankHQ

[Verifies balance]

[Deduct $100]

User collects $100

Send request (W,100)

Send Verified Msg

Display and Print new Bal

Chooses (Withdraw $100)

[Counts out $100]

Send ack of withdrawal

[encode]

network

Page 18: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 18

Copyright © 2007-9 by Leong Hon Wai

What if something

goes wrong?

Page 19: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 19

Copyright © 2007-9 by Leong Hon Wai

Simple ATM Scenario: Malfunction

Bank User

ATMMachine

BankHQ

Inserts ATM card

Prompt for PIN #

Enter the PIN #

Send encoded Acct #, PIN #

Send encoded Verified Msg

Display Options

Choose (Withdraw $100)

[Verifies A/C # & Pin #]

[Reads card]

[encode]

network

What if something goes wrong?

Page 20: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 20

Copyright © 2007-9 by Leong Hon Wai

Simple ATM Scenario: Malfunction…

Bank User

ATMMachine

BankHQ

[Verifies balance]

[Deduct $100]

User collects $100

Send request (W,100)

Send Verified Msg

Display and Print new Bal

Chooses (Withdraw $100)

[Counts out $100]

Send ack of withdrawal

[encode]

network

What if something goes wrong?

Page 21: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 21

Copyright © 2007-9 by Leong Hon Wai

Actually, no technical solution…

Page 22: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 22

Copyright © 2007-9 by Leong Hon Wai

What to modify/add for future…

Value added Services: Data Mining – frequent patterns Targeted marketing (Database marketing) Credit-card fraud, Handphone acct churning analysis

Page 23: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 23

Copyright © 2007-9 by Leong Hon Wai

Topic Summary: Database

Databases: what they are, organization, etc Database Query:

How query is processed “under the hood” 3 basic DB primitives Impetus for “Declarative Style of Query”

Concurrency Issues in Databases How to deal with Concurrency The ATM Case Study

Database Applications…

Page 24: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 24

Copyright © 2007-9 by Leong Hon Wai

Thank you!

Page 25: Database – Info Storage and Retrieval

LeongHW, SOC, NUS(UIT2201:3 Database) Page 25

Copyright © 2007-9 by Leong Hon Wai