1/38
databasesdatabasesdatabasesdatabases
What are transactions?
A sequence of SQL statements one after the other
What are the problems with transactions?
Many transactions may happen at the same time, and computers can fail
How do SQL queries behave in practice?
They do not exist in isolation
Many queries overlap in time
Queries may be coming from many computers at once
Computers can fail, leading to partial execution of SQL statements
What do transitions begin with?
What do transactions end with?
START TRANSACTION
END TRANSACTION
Why is COMMIT always necessary?
All changes to the database are tentative before COMMIT
What is the special case for transactions?
Each individual SQL statement is a transaction.
What do transactions ensure?
How are transactions executed?
What else can we state about transactions?
Serialisable behaviour
As if they are being executed in isolation from all other transactions
That it must be executed atomically
Translate the following into a low-level operation:
SELECT salary FROM Employees WHERE e_id = 1234;
UPDATE Employees SET salary = salary*1.1 WHERE e_id = 1234;
read(e_id=1234, salary);
salary = salary*1.1;
write(e_id=1234, salary);
Abstract the following:
read(e_id=1234, salary);
salary = salary*1.1;
write(e_id=1234, salary);
read(X);
X=X*1.1;
write(X);
What does the command read(X) do?
Explain this process
Reads a database item X into a program variable
Find the address of the disk block that contains item X
Copy that disk block into a buffer in main memory (if that disk block is not already in some main memory buffer)
Copy item X from the buffer into the program variable X
What does the command write(X) do?
Explain this process
Writes the value of the program variable X into the database item named X
find the address of the disk block that contains item X
copy that disk block into a buffer in main memory (if that disk block is not already in some main memory buffer)
If it is not already in some main memory buffer, copy item X from the program variable X into its correct location in the buffer
Store the updated block from the buffer back into disk either immediately or at some point later in time
What must the schedule preserve?
What are the two types of schedule?
What are the differences between these types of schedule?
That the operations in each transaction happen in the correct order
Serial and concurrent
Serial schedules execute transactions one after the other, whereas concurrent transactions can interleave operations from different transactions
Give the shorthand notation for the following:
Schedule
read(X) in transaction i
write(X) in transaction i
Commit in transaction i
Abort (rollback) in transaction i
Sid
ri(X)
wi(X)
ci
ai
Are the following schedules serial or concurrent?
r1(X); w1(X); r2(X); r1(Y); w1(Y); c1; r2(Y); c2
r2(X); r2(Y); w2(X); c2; r1(X); w1(X); r1(Y); w1(Y); c1
Concurrent
Concurrent and serial (all serial schedules are concurrent)
What does ACID stand for?
Explain the definition of each term
Atomicity, consistency, isolation, and durability
Transactions are an indivisible unit of execution, and are executed entirely, or not at all
A correct execution of a transaction must take the database from one consistent state to another
transactions may not violate constraints
it should correctly transform the database state to reflect the effect of a real-world event
A schedule satisfies isolation if it is serialisable
Once a transaction commits and changes the database, these changes cannot be lost because of subsequent failure
What are the causes of failure in a database?
What steps does the database take in the event of an abort?
User aborts transaction
System aborts transaction
Transaction aborts itself
System crashes, network failure etc
We undo the work done up to the error point, and the system recreates the database state as it was before the start of the aborted transaction
What are the 4 levels of isolation, and what do they mean?
Read uncommitted
essentially not having isolation at all
it is fine if you read data which has not been committed
Read committed
everything you read must have been committed before you can use it
Repeatable read
if you read the same thing twice in a transaction, you must get the same return value
Serialisable
default meaning of isolation
requires that all of the earlier levels are satisfied
What are the two components to transaction management?
What components are used to satisfy ACID?
Concurrency and control
Logging and memory
A - via recovery control, logging and recovery
C - via scheduler, concurrency control
I - via scheduler, concurrency control
D - via recovery control
List the properties of serial schedules
List the properties of concurrent schedules
Execute correctly, maintain consistency of the database, inefficient in multi-user environments (transactions have to wait until others are finished)
May not execute correctly, may not guarantee consistency of the database or isolation, efficient in multi-user environments
What is a serialisable schedule?
What do serialisable schedules guarantee?
What is the problem with serialisable schedules?
A schedule S is serialisable if there is a serial schedule S’ that has the same effect as S on every initial database state
Correctness and consistency
They are difficult to test
What is conflict-serialisability?
What is this based on?
Define this term
A stronger form of serialisability that is used/ enforced by most commercial DBMSs
Based on the notion of a conflict
A conflict in a schedule is a pair of operations from different transactions that cannot be swapped without changing the behaviour of at least one of the transactions (both access the same item, and at least one is a write operation)
What makes two schedules conflict-equivalent?
What makes a schedule conflict-serialisable?
Two schedules S and S’ are conflict-equivalent if S’ can be obtained from S by swapping any number of (1) consecutive (2) non-conflicting operations from (3) different transactions
A schedule is conflict-serialisable if you can in some way repeatedly swap consecutive pairs of non-conflicting operations from different transactions and get a serial schedule / the schedule is conflict-equivalent to a serial schedule
How is a precedence graph defined for a schedule S?
How do you test if a schedule S is conflict-serialisable?
The precedence graph for a schedule S is defined as follows:
it is a directed graph
its nodes are the transactions that occur in S
it has an edge from transaction Ti to transaction Tj if there is a conflicting pair of operations op1 and op2, such that:
op1 appears before op2 in S
op1 belongs to transaction Ti
op2 belongs to transaction Tj
Construct the precedence graph for S
If the precedence graph is not a cycle, then S is conflict-serialisable
Otherwise not
What is a simple locking mechanism?
How are these locks achieved?
What are the conditions of these locks?
A transaction has to lock an item before it accesses it
Locks are requested from and granted by the scheduler
Each item is locked by at most one transaction at a time, and transactions must wait until a lock can be granted. Each lock has to be released (unlocked) eventually
What is the syntax for a lock and an unlock?
What are the rules for these locks?
li(X) - transaction i requests a lock for item X
ui(X) - transaction I unlocks item X
For each ri(X)/wi(X) there is an earlier li(X) without any ui(X) occurring between li(X) and ri(X)/wi(X)
For each li(X) there is a later ui(X)
If li(X) comes before lj(X), then ui(X) occurs between li(X) and lj(X)
What is two-phase locking?
What is the condition for two-phase locking?
A modification of the simple locking mechanism that guarantees conflict-serialisability
In each transaction, all lock operations precede all unlocks
How do you test if a transaction is 2PL?
How can you tell whether a schedule is 2PL?
What does a 2PL schedule ensure?
Find the first unlock:
phase 1 is up to, but not including, that unlock
phase 2 is from that unlock until the end
A transaction is 2PL if phase 2 contains no lock
A schedule is 2PL if all transactions are 2PL
Conflict-serialisability
What problems could 2PL lead to?
Define this problem
How can we make 2PL more flexible?
Deadlocks
Transactions get stuck because multiple transactions are waiting for one of the others to release a lock
Implement different lock modes
List two types of locks
Define these, and their rules
Are there any additional rules?
Shared lock, exclusive lock
Shared lock: requested by transactions to read item x, granted to several transactions at the same time
Exclusive lock: requested by transactions to write to item x, granted to at most one transaction at a time
Shared lock on x is granted only if no other transactions hold an exclusive lock on x, exclusive lock on x is granted only if no other transactions have a lock on x of any kind
What is the shorthand notation for the following?
Transaction i requests a shared lock for item x
Transaction i requests an exclusive lock for item x
Transaction i requests an unlock for item x
sli(X)
xli(X)
ui(X)
What are the problems with upgrading locks?
What does it mean to upgrade a lock?
How is this used?
Risk of deadlocks
A shared lock on an item X can later be upgraded to an exclusive lock on X
This is used to be friendly to other transactions
What is a solution to deadlocks?
How do these work?
What does this mean for shared locks?
Update locks
Requested by transactions to read (not write) an item, may be upgraded later to an exclusive lock. Granted to at most one transaction at a time
They can no longer be upgraded
What is the standard principle of 2PL with shared, exclusive, and update locks?
Describe each phase of 2PL with shared, exclusive, and update locks
What does this guarantee?
In each transaction, all lock operations precede all unlocks
Phase 1: request locks and possibly other read/write operations
Phase 2: unlock and possibly other read/write operations
Conflict-serialisability
List the different levels of granularity at which the DBMS may use locks
Relations
Disk blocks
Tuples