Transactions Flashcards

1
Q

What are transactions?

A

A transaction is a unit of program execution that accesses and, possibly updates, various data item

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the main issues to deal with in terms of transactions?

A

◦ Failures of various kinds, such as hardware failures and system crashes.
◦ Concurrent execution of multiple transactions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the properties required for a transaction?

A

ACID - Atomicity, Consistency, Isolation, Durablity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does Atomicity mean?

A

Atomicity guarantees that each transaction is treated as a single unit, which either succeeds completely, or fails completely
◦ If any of the statements constituting a transaction fails to complete, the entire transaction fails and the database is left unchanged
◦ Atomicity must be guaranteed in every situation, including power failures, errors and crashes The system should ensure that updates of a partially executed transaction are
not reflected in the database

In short “All or Nothing Transactions”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does Consistency mean?

A

In general, consistency requirements include
. Explicitly specified integrity constraints
− primary keys and foreign keys

. Implicit integrity constraints
− sum of balances of all accounts, minus sum of loan amounts must equal value of cash-in-hand

Consistency ensures that a transaction can only bring the database from one valid state to
another, maintaining database invariants
◦ Any data written to the database must be valid according to all defined rules, including constraints,
cascades, triggers, and any combination thereof

In short “Guarantees Committed Transaction State”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does Isolation Requirement mean?

A

Transactions are often executed concurrently (multiple transactions reading and writing to a table at the same time)
◦ Isolation ensures that concurrent execution of transactions leaves the database in the same state
that would have been obtained if the transactions were executed sequentially

In short “Transactions are independent”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does Durability requirement mean?

A

Durability guarantees that once a transaction has been committed, it will remain committed
even in the case of a system failure (like power outage or crash)
◦ This usually means that completed transactions (or their effects) are recorded in non-volatile memory

In short “Committed data is never lost”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the various transaction states?

A

Every transaction can be in one of the following states (like Process States in OS)
◦ Active -The initial state; the transaction stays in this state while it is executing
◦ Partially committed -After the final statement has been executed
◦ Failed - After the discovery that normal execution can no longer proceed
◦ Aborted - After the transaction has been rolled back and the database restored to its state prior to the start of the transaction. Two options after it has been aborted:
− Restart the transaction: Can be done only if no internal logical error
− Kill the transaction
◦ Committed - After successful completion
◦ Terminated - After it has been committed or aborted (killed)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are concurrent executions?

A

Multiple transactions are allowed to run concurrently in the system. Advantages are:
1. Increased processor and disk utilization
2. Reduced average response time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what are concurrency control schemes?

A

Mechanisms to achieve isolation
◦ To control the interaction among the concurrent transactions in order to prevent them from destroying the consistency of the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a schedule?

A

Schedule: A sequence of instructions that specify the chronological order in which instructions of concurrent transactions are executed. A schedule for a set of transactions must consist of all instructions of those
transactions. Must preserve the order in which the instructions appear in each individual transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When will a transaction complete?

A

A transaction that successfully completes its execution will have a commit instruction as the last statement.
◦ By default, transaction assumed to execute commit instruction as its last step. A transaction that fails to successfully complete its execution will have an abort
instruction as the last statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a serial schedule?

A

A schedule is said to be serial schedule if only after the commit of one transaction, the other transaction begins.

A (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule and is free from inconsistency problems. In a serial schedule, transactions are executed one after the other.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the problem with concurrent schedules?

A

Though it has better utilization of resources, some of the transactions could leave the database in an inconsistent state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to solve the inconsistency problems associated with the concurrent schedules?

A

By converting the concurrent schedule into an equivalent schedule which is serializable.

If this is possible, we call such a concurrent schedule as a “Serializable schedule”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are different forms of schedule equivalence?

A

a) Conflict Serializability
b) View Serializability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a conflict serializable schedule?

A

A Schedule is said to be “Conflict serializable” , if one of its conflict equivalent schedule is serializable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a conflict equivalent schedule?

A

If a schedule S can be transformed into a schedule S’ by a series of swaps of non-conflicting instructions, we say that S and S’ are conflict equivalent.

  • If we swap conflicting pairs, then the output would change and could be inconsistent and hence the schedule may not remain “Equivalent”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What are conflicting instructions?

A

Let li and lj be two Instructions from transactions Ti and Tj respectively
* Instructions li and lj conflict if and only if there exists some item Q accessed by both li and lj, and at least one of these instructions write to Q
a) li = read(Q), lj = read(Q). li and lj don’t conflict
b) li = read(Q), lj = write(Q). They conflict
c) li = write(Q), lj = read(Q). They conflict
d) li = write(Q), lj = write(Q). They conflict
* Intuitively, a conflict between li and lj forces a (logical) temporal order between them
◦ If li and lj are consecutive in a schedule and they do not conflict, their results would remain the same even if they had been interchanged in the schedule

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are conflict pairs ?

A

conflict pairs - 1) R(A), W(A) or W(A), R(A)
2) W(A),R(A) or R(A)W(A)
3) W(A),W(A)
Order doesn’t matter among them, no matter in which order they exist, they are problematic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are non conflict pairs?

A

The following tranasactions do not affect the consistency of the database. Hence are swappable.
1. R(A)R(B)
2. R(A)R(A)
3. W(A), W(B)
4. R(A),W(B)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How to find Conflict Equivalent Schedule?

A

We swap non conflicting pairs and try to bring a serial order. Thus we have converted an concurrent schedule into a conflict Equvivalent Schedule aka we have made the concurrent schedule serializable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
  • Are all serializable schedules conflict-serializable?
A

No.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is conflict serializable schedule?

A

We say that a schedule S is conflict serializable if it is conflict equivalent to a serial schedule.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What is a quicker method to check serializabiliity? (Conflict Serializability)

A

Precedence Graph Method. Nodes represent transactions, edges represent various conflicts that occur between various transactions in a schedule.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What are precedence graph?

A

A direct graph where the vertices are the transactions (names). We draw an arc/edge from Ti to Tj if the two transactions conflict, and Ti accessed the data item on which the conflict arose earlier. We may label the arc by the item that was accessed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How to test for conflict serializability?

A

A schedule is conflict serializable if and only if its precedence graph is acyclic. Else, it is Non-conflict serializable.

Important Point: We say that the schedule is Non-Conflict Serializable, but we do not say that it is non-serializable.

28
Q

What is the complexity of graph cycle-detection algorthims

A

Cycle-detection algorithms exist which take order n^2
time, where n is the number of vertices in the graph
◦ (Better algorithms take order n + e where e is the
number of edges)

29
Q

How to obtain the serializability order from precedence graph?

A

If precedence graph is acyclic, the serializability order can
be obtained by a topological sorting of the graph.
1. Remove the node with an Indegree of 0 and also remove all the edges corresponding to it.
2. Repeat Step1 till all the nodes are covered.
3. The final order of the execution and transactions is the order in which the nodes are removed from the graph.

30
Q

When do we say schedule is non - serializable?

A

to test serializability , we first check for conflict-serializablity? If yes, we say it is seriablizable, If no, then we check for view serialzable? If yes, we say it is serializable. If no, then we could conclude that the schedule is non-serializable.

31
Q

What is view serializablility?

A

A schedule is said to be view serializable if it has an equivalent “View Equivalent” Schedule.

32
Q
A
33
Q

What is recovery?

A

if system fails after Step 3 and before Step 6?
◦ Leads to inconsistent state
◦ Need to rollback update of A
* This is known as Recovery

34
Q

What are Recoverable Schedules?

A

If a transaction Tj reads a data item previously written by a transaction Ti , then the commit operation of Ti must appear before the commit operation of Tj.
.

35
Q

How to find if a schedule is recoverable?

A

if a transaction reading an item gets committed before the transaction which as written the data item. then the schedule is IRRECOVERABLE.

36
Q

What are cascading rollback?

A

A single transaction failure leads to a series of transaction
rollbacks.

37
Q

What are cascadeless schedules?

A
  • Cascadeless schedules: For each pair of transactions Ti and Tj such that Tj reads a data item previously written by Ti
    , the commit operation of Ti appears before the read
    operation of Tj.
  • Every cascadeless schedule is also recoverable
  • It is desirable to restrict the schedules to those that are cascadeless
38
Q

What are Transaction Control Language (TCL)?

A

The following commands are used to control transactions
◦ COMMIT
. To save the changes
◦ ROLLBACK
. To roll back the changes
◦ SAVEPOINT
. Creates points within the groups of transactions in which to ROLLBACK
◦ SET TRANSACTION
. Places a name on a transaction

39
Q

How is COMMIT Command used?

A

The syntax for the COMMIT command is as follows:
◦ SQL> DELETE FROM Customers WHERE AGE = 25;
◦ SQL> COMMIT;

40
Q

How is ROLLBACK Command used?

A

The syntax for a ROLLBACK command is as follows:
◦ SQL> DELETE FROM Customers WHERE AGE = 25;
◦ SQL> ROLLBACK;

41
Q

How is SAVEPOINT / ROLLBACK Command?

A

SAVEPOINT SAVEPOINT NAME;
ROLLBACK TO SAVEPOINT NAME;

42
Q

How is RELEASE SAVEPOINT Command used?

A

RELEASE SAVEPOINT SAVEPOINT NAME;

43
Q

How is SET TRANSACTION Command used?

A

SET TRANSACTION [ READ WRITE | READ ONLY ];

44
Q

What is view serializability?

A

Let S and S’ be two schedules with the same set of transactions. S and S’ are view equivalent if the following three conditions are met, for each data item Q,
◦ Initial Read: If in schedule S, transaction Ti reads the initial value of Q, then in schedule S’ also transaction Ti must read the initial value of Q
◦ Write-Read Pair: If in schedule S transaction Ti executes read(Q), and that value was produced by transaction Tj (if any), then in schedule S’ also transaction Ti must read the value of Q that was produced by the same write(Q) operation of transaction Tj
◦ Final Write: The transaction (if any) that performs the final write(Q) operation in schedule S must also perform the final write(Q) operation in schedule S’

A schedule S is view serializable if it is view equivalent to a serial schedule

Every conflict serializable schedule is also view serializable

Every view serializable schedule that is not conflict serializable has blind writes

45
Q

Test for View Serializability?

A

The problem of checking if a schedule is view serializable falls in the class of NP-complete problems.

However, practical algorithms that just check some sufficient conditions for view serializability can still be used

  1. Check whether the schedule is view serializable or not?
46
Q

What are preferred in terms of serializability and recoverability in a database?

A

A database must provide a mechanism that will ensure that all possible schedules are both:
◦ Conflict serializable
◦ Recoverable and, preferably, Cascadeless

47
Q

What are lock-based protocols?

A
  • A lock is a mechanism to control concurrent access to a data item
  • Data items can be locked in two modes:
    a) exclusive (X) mode:
    ◦ Data item can be both read as well as written
    ◦ X-lock is requested using lock-X instruction
    b) shared (S) mode:
    ◦ Data item can only be read
    ◦ S-lock is requested using lock-S instruction
  • A transaction can unlock a data item Q by the unlock(Q) Instruction
  • Lock requests are made to the concurrency-control manager by the programmer
  • Transaction can proceed only after request is granted
48
Q

What is a lock-compatibility matrix?

A

A lock compatibility matrix is used which states whether
a data item can be locked by two transactions at the same time

49
Q

What is a deadlock?

A

Deadlock is a state where neither of these transactions can ever proceed with its normal execution
* This situation is called deadlock

When deadlock occurs, the system must roll back one of the
two transactions.
* Once a transaction has been rolled back, the data items that were locked by that transaction are unlocked.
* These data items are then available to the other transaction, which can continue with its execution

50
Q

Given Deadlocks and inconsistent states of database, what is preferred?

A

Deadlocks are definitely preferable to inconsistent states, since they can be handled by rolling back transactions, whereas inconsistent states may lead to real-world problems that cannot be handled by the database system

51
Q

What is a locking protocol?

A

A locking protocol is a set of rules followed by all transactions while requesting and releasing locks* The set of all such schedules is a proper subset of all possible serializable schedules * We present locking protocols that allow only conflict-serializable schedules, and thereby ensure isolation

52
Q

What are Two-Phase Locking Protocol?

A

This protocol ensures conflict-serializable schedules
* Phase 1: Growing Phase
◦ Transaction may obtain locks
◦ Transaction may not release locks
* Phase 2: Shrinking Phase
◦ Transaction may release locks
◦ Transaction may not obtain locks

53
Q

What are lock conversions?

A

Two-phase locking with lock conversions:
– First Phase:
. can acquire a lock-S on item
. can acquire a lock-X on item
. can convert a lock-S to a lock-X (upgrade)
– Second Phase:
. can release a lock-S
. can release a lock-X
. can convert a lock-X to a lock-S (downgrade)
* This protocol assures serializability. But still relies on the programmer to insert the various locking instruction

54
Q

Does 2 phase locking ensure freedom from deadlocks?

A

No

55
Q

What is Starvation?

A

Starvation occurs if the concurrency control manager is badly designed. For example:
◦ A transaction may be waiting for an X-lock on an item, while a sequence of other transactions request and are granted an S-lock on the same item
◦ The same transaction is repeatedly rolled back due to deadlocks
* Concurrency control manager can be designed to prevent starvation
* Starvation is also loosely referred to as Livelock

56
Q

What are cascading rollback?

A
  • When a deadlock occurs there is a possibility of cascading roll-backs
  • Cascading roll-back is possible under twophase locking
    To avoid Cascading roll-back, follow a modified protocol called strict two-phase locking
    ◦ a transaction must hold all its exclusive locks till it commits/aborts
57
Q

What is rigorous two-phase locking?

A

Rigorous two-phase locking is even stricter
◦ All locks are held till commit/abort. In this protocol transactions can be serialized
in the order in which they commit
* Note that concurrency goes down as we move to more and more strict locking protocol

58
Q

Implementation of locking?

A

A lock manager can be implemented as a separate process to which transactions send lock and unlock requests

The lock manager maintains a data-structure called a lock table to record granted locks and pending requests

The lock table is usually implemented as an in-memory hash table indexed on the name of the data item being locked

59
Q

What are deadlock prevention protocols?

A

Deadlock Prevention protocols ensure that the system will never enter into a deadlock state. Some prevention strategies:
◦ Require that each transaction locks all its data items before it begins execution (pre-declaration)
◦ Impose partial ordering of all data items and require that a transaction can lock data items only in the order specified by the partial order

60
Q

What are Transaction Timestamp?

A

Transaction Timestamp: Timestamp is a unique identifier created by the DBMS to
identify the relative starting time of a transaction. Timestamping is a method of
concurrency control in which each transaction is assigned a transaction timestamp

61
Q

What are the schemes which use transaction timestamps for the sake of deadlock prevention alone?

A

wait-die scheme: non-preemptive
. Older transaction may wait for younger one to release data item. (older means smaller timestamp)
− Younger transactions never wait for older ones; they are rolled back instead
. A transaction may die several times before acquiring needed data item

◦ wound-wait scheme: preemptive
. Older transaction wounds (forces rollback) of younger transaction instead of waiting for it
− Younger transactions may wait for older ones
. May be fewer rollbacks than wait-die scheme

62
Q

What are Timeout Based Schemes?

A

◦ A transaction waits for a lock only for a specified amount of time. If the lock has not been granted within that time, the transaction is rolled back and restarted
◦ Thus, deadlocks are not possible
◦ Simple to implement; but starvation is possible. Also difficult to determine good value of the timeout interval

63
Q

How to detect deadlocks?

A

Deadlocks can be described as a wait-for graph, which consists of a pair G = (V, E),
◦ V is a set of vertices (all the transactions in the system)
◦ E is a set of edges; each element is an ordered pair Ti → Tj
.
The system is in a deadlock state if and only if the wait-for graph has a cycle
* Must invoke a deadlock-detection algorithm periodically to look for cycles

64
Q

What are the steps in deadlock recovery?

A

When deadlock is detected:
◦ Some transaction will have to rolled back (made a victim) to break deadlock. Select
that transaction as victim that will incur minimum cost
◦ Rollback – determine how far to roll back transaction
. Total rollback: Abort the transaction and then restart it
. More effective to roll back transaction only as far as necessary to break deadlock
◦ Starvation happens if same transaction is always chosen as victim. Include the number of rollbacks in the cost factor to avoid starvation

65
Q

What are Timestamp based protocols?

A

The protocol manages concurrent execution such that the time-stamps determine the serializability order
In order to assure such behavior, the protocol maintains for each data Q two timestamp values:
◦ W-timestamp(Q) is the largest time-stamp of any transaction that executed write(Q) successfully

◦ R-timestamp(Q) is the largest time-stamp of any transaction that executed read(Q) successfully

The timestamp ordering protocol ensures that any conflicting read and write operations are executed in timestamp order.

The timestamp-ordering protocol guarantees serializability since there will be no cycles in the precedence graph.

Timestamp protocol ensures freedom from deadlock as no transaction ever waits
* But the schedule may not be cascade-free, and may not even be recoverable