PHP Miscellaneous � $db->insert_id IT420: Database Management and � Retrieves the ID generated for an Organization AUTO_INCREMENT column by the previous INSERT query � Return value: Managing Multi-user � The ID generated for an AUTO_INCREMENT column by the previous INSERT query on success Databases � 0 if the previous query does not generate an (Chapter 9) AUTO_INCREMENT value � FALSE if no MySQL connection was established. 1 Kroenke, Database Processing 2 Goals Database Administration � All large and small databases need database � Database Administration administration � Concurrency Control � Barber Shop database (small DB) � Large, multi-user DB Kroenke, Database Processing 3 Kroenke, Database Processing 4 1
DBA Tasks Managing Database Structure � Managing database structure � Participate in database and application � Controlling concurrent processing development � Managing processing rights and responsibilities � Developing database security � Facilitate changes to database structure � Providing for database recovery � Managing the DBMS � Maintaining the data repository � Maintain documentation � Who do people blame if something goes wrong? Kroenke, Database Processing 5 Kroenke, Database Processing 6 DBA Tasks Concurrency Control � Managing database structure � Concurrency control: ensure that one � Controlling concurrent processing user’s work does not inappropriately � Managing processing rights and responsibilities influence another user’s work � Developing database security � Providing for database recovery � Managing the DBMS � Maintaining the data repository Kroenke, Database Processing 7 Kroenke, Database Processing 8 2
Errors Introduced Without Atomic Transactions Atomic Transaction � A transaction , or logical unit of work (LUW) , is a series of actions taken against the database that occurs as an atomic unit � Either all actions in a transaction occur - COMMIT � Or none of them do – ABORT / ROLLBACK Kroenke, Database Processing 9 Kroenke, Database Processing 10 Errors Prevented With Class Exercise Atomic Transaction � Example of transaction in the Online Mids Store Application – submit order Make changes permanent Undo changes Kroenke, Database Processing 11 Kroenke, Database Processing 12 3
Other Transaction Examples? Concurrent Transaction � Concurrent transactions: transactions that appear to users as they are being processed at the same time � In reality, CPU can execute only one instruction at a time � Transactions are interleaved � Concurrency problems � Lost updates � Inconsistent reads Kroenke, Database Processing 13 Kroenke, Database Processing 14 Lost Update Problem Concurrent Transaction Processing User 1: Buy 10 Snicker bars User 1: Buy 10 Snicker bars User 2: Buy 2 Gatorade bottles User 2: Buy 2 Snicker bars User 1: User 2: User 1: User 2: Read nb Snickers (ns=500) Read nb Gatorades (ng=200) Read nb Snickers (ns=500) Read nb Snickers (ns2=500) Reduce count Snickers by 10 (ns=490) Reduce count Gatorades by 2 (ng=198) Reduce count Snickers by 10 (ns=490) Reduce count Snickers by 2 (ns2=498) Write new nb Snickers back (ns=490) Write new nb Gatorades back (ng=198) Write new nb Snickers back (ns=490) Write new nb Snickers back (ns2=498) Possible order of processing at DB server: Order of processing at DB server: • Read nb Snickers (ns=500) U1: Read nb Snickers (ns=500) • Read nb Gatorades (ng=200) U2: Read nb Snickers (ns2=500) • Reduce count Snickers by 10 (ns=490) U1: Reduce count Snickers by 10 (ns=490) • Write new nb Snickers back (ns=490) U1: Write new nb Snickers back (ns=490) • Reduce count Gatorades by 2 (ng=198) U2: Reduce count Snickers by 2 (ns2=498) • Write new nb Gatorades back (ng=198) U2: Write new nb Snickers back (ns2=498) Kroenke, Database Processing 15 Kroenke, Database Processing 16 4
DBMS’s View Inconsistent-Read Problem U1: Read nb Snickers (ns=500) T1: R(Snickers) � Dirty reads – read uncommitted data U2: Read nb Snickers (ns2=500) T2: R(Snickers) U1: Reduce count Snickers by 10 (ns=490) � T1: R(A), W(A), R(B), W(B), Abort U1: Write new nb Snickers back T1: W(Snickers) � T2: R(A), W(A), Commit (ns=490) T1: COMMIT U2: Reduce count Snickers by 2 (ns2=498) T2: W(Snickers) � Unrepeatable reads U2: Write new nb Snickers back T2: COMMIT (ns2=498) time � T1: R(A), R(A), W(A), Commit T1: R(S) W(S) Commit � T2: R(A), W(A), Commit T2: R(S) W(S) Commit time Kroenke, Database Processing 17 Kroenke, Database Processing 18 Class Exercise Inconsistent Read Example � Transaction Steps � Possible Schedule � Possible Problems � T1: Transfer money from savings to checking � T2: Add interest for savings account Kroenke, Database Processing 19 Kroenke, Database Processing 20 5
Resource Locking Lock Terminology � Implicit locks - placed by the DBMS � Locking: prevents multiple applications from � Explicit locks - issued by the application obtaining copies of the same resource when the program resource is about to be changed � Lock granularity - size of a locked resource � Rows, page, table, and database level � Types of lock � Exclusive lock (X) - prohibits other users from reading the locked resource � Shared lock (S) - allows other users to read the locked resource, but they cannot update it Kroenke, Database Processing 21 Kroenke, Database Processing 22 Explicit Locks Class Exercise – Place Locks User 1: Buy 10 Snicker bars � T1: R(Sa), W(Sa), R(Ch), W(Ch), Abort User 2: Buy 2 Snicker bars � T2: R(Sa), W(Sa), C User 1: User 2: Lock Snickers Lock Snickers Read nb Snickers (ns=500) Read nb Snickers (ns2=500) Reduce count Snickers by 10 (ns=490) Reduce count Snickers by 2 (ns2=498) Write new nb Snickers back (ns=490) Write new nb Snickers back (ns2=498) Order of processing at DB server: Kroenke, Database Processing 23 Kroenke, Database Processing 24 6
Serializable Transactions Strict Two-Phase Locking � Serializable transactions: � Strict two-phase locking � Run concurrently � Locks are obtained throughout the transaction � Results like when they run separately � All locks are released at the end of transaction (COMMIT or ROLLBACK) � Strict two-phase locking – locking technique to achieve serializability Kroenke, Database Processing 25 Kroenke, Database Processing 26 Strict 2PL Example Class Exercise – Place Locks � Strict 2PL � Not 2PL � T1: R(Sa), W(Sa), R(Ch), W(Ch) � X(A) � X(A) � T2: R(Ch), W(Ch), R(Sa), W(Sa) � R(A) � R(A) � W(A) � W(A) � Rel(A) � X(B) � X(B) � R(B) � R(B) � W(B) � W(B) � Rel(B,A) � Rel(B) Kroenke, Database Processing 27 Kroenke, Database Processing 28 7
Deadlock Deadlock � Deadlock : two transactions are each waiting on a resource that the other transaction holds � Prevent deadlocks � Break deadlocks Kroenke, Database Processing 29 Kroenke, Database Processing 30 Optimistic versus Pessimistic Optimistic Locking Locking � Optimistic locking assumes that no transaction conflict will occur � Pessimistic locking assumes that conflict will occur Kroenke, Database Processing 31 Kroenke, Database Processing 32 8
Pessimistic Locking Declaring Lock Characteristics � Most application programs do not explicitly declare locks due to its complication � Mark transaction boundaries and declare locking behavior they want the DBMS to use � Transaction boundary markers: BEGIN, COMMIT, and ROLLBACK TRANSACTION � Advantage � If the locking behavior needs to be changed, only the lock declaration need be changed, not the application program Kroenke, Database Processing 33 Kroenke, Database Processing 34 ACID Transactions Marking Transaction Boundaries � Transaction properties: � A tomic - all or nothing � C onsistent � I solated � D urable – changes made by commited transactions are permanent Kroenke, Database Processing 35 Kroenke, Database Processing 36 9
Consistency Statement Level Consistency � Consistency means either statement level or UPDATE CUSTOMER transaction level consistency SET AreaCode = ‘410’ � Statement level consistency : each statement WHERE ZipCode = ‘21218’ independently processes rows consistently � Transaction level consistency : all rows impacted by either of the SQL statements are protected from � All qualifying rows updated changes during the entire transaction � With transaction level consistency, a transaction may not see � No concurrent updates allowed its own changes Kroenke, Database Processing 37 Kroenke, Database Processing 38 Transaction Level Consistency ACID Transactions � A tomic Start transaction UPDATE CUSTOMER � C onsistent SET AreaCode = ‘425’ � I solated WHERE ZipCode = ‘21666’ � D urable ….other transaction work UPDATE CUSTOMER SET Discount = 0.25 WHERE AreaCode = ‘425’ End Transaction The second Update might not see the changes it made on the first Update Kroenke, Database Processing 39 Kroenke, Database Processing 40 10
Recommend
More recommend