Top Banner

of 25

Database – Info Storage and Retrieval

Feb 23, 2016

ReportDownload

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

Lecture Title

Database Info Storage and RetrievalAim: 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.3Optional: Some experiences with MySQL, AccessLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiCoffee Awards in UIT2201Coffee awards goes to interesting, creative things done in tutorials, good attempt on A-problems,interesting suggestions or comments in classClaim them anytimeJust email meTo get a coffee and chat

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency IssuesConcurrencyWhen 2 processes access S at the same time!Can cause all kinds of problemsImportant issue in all areasIllustrate with concurrency issue in databaseReadings for Concurrency IssuesRecord Locking Wikipedia http://en.wikipedia.org/wiki/Record_lockingRead [SG3] Section 6.4.1 (pp. 268--272) Efficient Allocation and Safe Use of ResourcesLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency Issue: ExampleBank 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 ProcessesBANK-ACCOUNT-DBAccount #NameBalanceOther Info2201-1022Albert Bank5000 LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiNormal Operations (1/2)Bank-Account-DB2201-10225000 Withdraw-P(1000);Deposit-P(2000);TWTDTime Withdraw-P(1000) Deposit(2000) BalanceTWCheck balance [5000]5000TW+1Bal Bal 10004000... . . . . . .4000TDCheck balance [4000]4000TD+1Bal Bal + 20006000If (TW < TD), thenCorrect balanceLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiNormal Operations (2/2)Bank-Account-DB2201-10225000 Withdraw-P(1000);Deposit-P(2000);TWTDTime Withdraw-P(1000) Deposit(2000) BalanceTDCheck balance [5000]5000TD+1Bal Bal + 20007000... . . . . . .7000TWCheck balance [7000]7000TW+1Bal Bal 10006000If (TW > TD), thenCorrect balanceLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency 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?LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency Problem (2/4)Bank-Account-DB2201-10225000 Withdraw-P(1000);Deposit-P(2000);TWTDTime Withdraw-P(1000) Deposit(2000) BalanceTDCheck balance [5000]5000TD+1Check balance [5000]5000TD+2 Bal Bal + 20007000TD+3Bal Bal 10004000If (TW = TD), and Deposit-process got in firstWrong balanceLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency Problem (2/3)Bank-Account-DB2201-10225000 Withdraw-P(1000);Deposit-P(2000);TWTDTime Withdraw-P(1000) Deposit(2000) BalanceTWCheck balance [5000]5000TW+1 Check balance [5000]5000TW+2Bal Bal 10004000TW+3Bal Bal + 20007000If (TW = TD), and Withdraw-process got in firstWrong balanceLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency Problem (3/3)Operations of the two processes are interleavedWithdraw-Process and Deposit-Processinterfere with each otherWrong balance for both casesSince one of the operations is over-written

LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency Solution: Lock operationIDEA: If one process P is changing balance, make sure that other processes do not access the same balance until P is doneSolution: 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.LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency 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 ProcessesBank-Account-DB2201-10225000 LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency Solution: (3/4)Bank-Account-DB2201-10225000 Withdraw-P(1000);Deposit-P(2000);TWTDTime Withdraw-P(1000) Deposit(2000) BalanceTWGet & Lock record;5000TW+1 Get...; [blocked]5000TW+2Bal Bal 1000;Unlock record;4000TW+3Get & Lock record;4000TW+4Bal Bal + 2000;Unlock record;6000If (TW = TD), and Withdraw-process got in firstLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiConcurrency Solution: (4/4)Bank-Account-DB2201-10225000 Withdraw-P(1000);Deposit-P(2000);TWTDTime Withdraw-P(1000) Deposit(2000) BalanceTWGet & Lock record;5000TW+1Get...; [blocked]5000TW+2Bal Bal + 2000;Unlock record;7000TW+3Get & Lock record;7000TW+4Bal Bal 1000;Unlock record;6000If (TW = TD), and Deposit-process got in firstLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiCase Study:The ATM Machine LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiSimple ATM Scenario (1 of 2)Bank User ATMMachineBankHQ Inserts ATM cardPrompt for PIN #Enter the PIN #Send encoded Acct #, PIN #Send encoded Verified MsgDisplay OptionsChoose (Withdraw $100)[Verifies A/C # & Pin #][Reads card] [encode] networkLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiSimple ATM Scenario (2 of 2)Bank User ATMMachineBankHQ [Verifies balance][Deduct $100]User collects $100Send request (W,100)Send Verified MsgDisplay and Print new BalChooses (Withdraw $100)[Counts out $100] Send ack of withdrawal[encode] networkLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiWhat if something goes wrong? LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiSimple ATM Scenario: MalfunctionBank User ATMMachineBankHQ Inserts ATM cardPrompt for PIN #Enter the PIN #Send encoded Acct #, PIN #Send encoded Verified MsgDisplay OptionsChoose (Withdraw $100)[Verifies A/C # & Pin #][Reads card] [encode] networkWhat if something goes wrong?LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiSimple ATM Scenario: MalfunctionBank User ATMMachineBankHQ [Verifies balance][Deduct $100]User collects $100Send request (W,100)Send Verified MsgDisplay and Print new BalChooses (Withdraw $100)[Counts out $100] Send ack of withdrawal[encode] networkWhat if something goes wrong?LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiActually, no technical solutionLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiWhat to modify/add for futureValue added Services: Data Mining frequent patterns Targeted marketing (Database marketing) Credit-card fraud, Handphone acct churning analysisLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon WaiTopic Summary: DatabaseDatabases: what they are, organization, etcDatabase Query: How query is processed under the hood3 basic DB primitivesImpetus for Declarative Style of QueryConcurrency Issues in DatabasesHow to deal with ConcurrencyThe ATM Case StudyDatabase ApplicationsLeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon Wai

Thank you!LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon Wai*07/16/96*##LeongHW, SOC, NUS(UIT2201:3 Database) Page #Copyright 2007-9 by Leong Hon Wai