-------------------------------------------------------------------------------- LAB 4 | Lock-based protocols, 2PL. -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- PRACTICE EXERCISES -------------------------------------------------------------------------------- (1) Get familiar with the terms: - locks (shared, exclusive), - lock compatibility, - locking protocol, - two-phase locking, - strict two-phase locking, by attending the lecture and reading chapters 15.1.1, 15.1.2, 15.1.3 from the textbook. (2) Add lock and unlock instructions to transactions T1 and T2 such that they conform to the two-phase locking phases. T1: T2: read(X) read(Y) read(Y) read(X) if X=0 then Y:=Y+1 if Y=0 then X:=X+1 write(Y) write(X) (3) Consider the following schedule. T1: T2: read(Y) read(Y) Y:=Y-50 write(Y) read(X) read(X) print(X+Y) X:=X+50 write(X) print(X+Y) (a) Add lock and unlock instructions to transactions T1 and T2 such that they conform to the two-phase locking phases. (b) Can the following schedule be the output of a two-phase locking scheduler? If so, show the schedule with all required lock and unlock instructions. Otherwise explain why. (4) Can the following schedules be the output of a two-phase locking scheduler? If so, show the schedules with all required lock and unlock instructions. Otherwise explain why. Schedule 1 Schedule 2 --------------------------- --------------------------- T1: T2: T3: T1: T2: T3: read(Z) read(X) read(X) read(Y) read(Z) read(Z) write(Z) read(Y) read(X) write(X) read(Y) write(Z) write(X) write(Y) read(X) write(Y) read(Y) (5) Consider the following schedule. T1: T2: T3: read(X) read(Y) read(X) read(X) write(X) read(Z) write(Z) COMMIT write(Y) COMMIT read(V) COMMIT (a) Can the following schedule be the output of a strict two-phase locking scheduler? If so, show the schedule with all required lock and unlock instructions. Otherwise explain why. (b) Assume read(X) in T2 is moved immediately after write(X) in T3. Can such schedule be then the output of a strict two-phase locking scheduler? If yes, add all required lock and unlock instructions. If no, explain why. (6) FURTHER READING: - 'Two-phase locking' from Encyclopedia of Database Systems [1]. - 'Explicit Locking' in PostgreSQL documentation [2]. - 'Data Consistency Checks at the Application Level' in PostgreSQL documentation [3]. (7) CHECKLIST: - I can explain the purpose of the locking mechanism for concurrent transactions. - I can list different types of locks and explain their compatibility. - I can explain the mechanism of the two-phase locking protocol and show it on an example. -------------------------------------------------------------------------------- HOMEWORK -------------------------------------------------------------------------------- DEADLINE: 05.12.2016, 21:59 In this exercise we make our monsters to eat the cookies concurrently. Consider the script 'cookie_jar_h4.py'. Your task is to implement the logics of eating a cookie inside the definition of the function (search for 'TODO') 'monster_eat_cookie(eating_transaction_mode, debug)' where, 'eating_transaction_mode' specifies if the cookie should be eaten using a 'single' transaction or a 'double' transaction. (This functionality is supported by command-line option '--eating-transaction-mode' or '-e'. 'single' : A monster finds all cookies available and eats the first one, all in a single transaction. 'double' : In the first transaction, a monster finds all cookies available. In the second transaction, a monster eats the first available cookie it found. The flag 'debug' can be used to do anything in a debug mode triggered by the '-d' or '--debug' command-line option. For the evaluation, I will execute your script with varying parameters. We'll discuss the results of your script during the labs. -------------------------------------------------------------------------------- REFERENCES -------------------------------------------------------------------------------- [1] (Access from university network.) https://link.springer.com/referencework/10.1007%2F978-0-387-39940-9 [2] https://www.postgresql.org/docs/current/static/explicit-locking.html [3] https://www.postgresql.org/docs/current/static/applevel-consistency.html