Lecture 08 Concurrency Control Part I (Transactions)
作者:互联网
Transaction Concept:
A transaction is a unit of program execution that accesses and possibly updates various data items.
A transaction is the execution of a sequence of one or more operations (e.g., SQL queries) on a database to perform some higher-level function.
It is the basic unit of change in a DBMS
E.g. transaction example:
Move $50 from account A to account B
Transaction:
Check whether A has $50
Deduct $50 from A
Add $50 to B
Example of Fund Transfer:
Transaction to transfer $50 from account A to account B:
1. read(A)
2. A:=A–50
3. write(A)
4. read(B)
5. B:=B+50
6. write(B)
Atomicity requirement:
if the transaction fails after step 3 and before step 6, money will be “ lost” leading to an inconsistent database state
Failure could be due to software or hardware
the system should ensure that updates of a partially executed transaction
are not reflected in the database
Durability requirement :
once the user has been notified that the transaction has completed (i.e., the transfer of the $50 has taken place), the updates to the database by the transaction must persist even if there are software or hardware failures.
Consistency requirement in above example:
the sum of A and B is unchanged by the execution of the transaction
In general, consistency requirements include
Explicitly specified integrity constraints such as primary keys and foreign keys
Implicit integrity constraints: e.g.sum of balances of all accounts,minus sum of loan amounts must equal value of cash-in-hand
A transaction must see a consistent database.
During transaction execution the database may be temporarily inconsistent.
When the transaction completes successfully the database must be consistent
Other requirement:
ACID Properties:
Transaction State:
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.
Schedules:
Schedule – a sequences 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.
A transaction that successfully completes its execution will have a commit instructions 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
Serial Schedule:
Non-serial Schedule(But Equivalent to the previous one):
Non-serial Schedule(don't preserve the value of A+B):
Problem Statement:
Arbitrary interleaving of operations can lead to:
Temporary Inconsistency (ok, unavoidable) Permanent Inconsistency (BAD!)
We need formal correctness criteria to determine whether an interleaving is valid.
DBMS achieves concurrency by interleaving the actions (R/W of DB objects) of txns.
We need a way to interleave txns but still make it appear as if they ran one-at-a-time
Serializable schedule
Serializability:
Basic Assumption – Each transaction preserves database consistency.
Thus serial execution of a set of transactions preserves database consistency.
A (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule. Different forms of schedule equivalence give rise to the notions of:
1. conflict serializability
2. view serializability(won't be covered)
Simplified view of transactions:
We ignore operations other than read and write instructions
We assume that transactions may perform arbitrary computations on data in local buffers in between reads and writes.
Our simplified schedules consist of only read and write instructions.
Conflicting Instructions:
Instructions li and lj of transactions Ti and Tj respectively, conflict if and only if there exists some item Q accessed by both li and lj, and at least one of these instructions wrote Q.
1. li = read(Q), lj = read(Q). li and lj don’ t conflict.
2. li = read(Q), lj = write(Q). They conflict.
3. li = write(Q), lj = read(Q). They conflict
4. 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
Conflict Serializability:
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.
We say that a schedule S is conflict serializable if it is conflict equivalent to a serial schedule
Example of conflict equivalent:
Example of conflict Serializable:
Example of a schedule that is not conflict serializable:
Testing for Serializability:
Consider some schedule of a set of transactions T1, T2, ..., Tn
Precedence graph — a direct graph where the vertices are the transactions (names).
We draw an arc from Ti to Tj if the two transaction conflict, and Ti accessed the data item on which the conflict arose earlier.
We may label the arc by the item that was accessed.
Some examples of precedence graph:
Recoverable Schedules:
Need to address the effect of transaction failures on concurrently running transactions.
Recoverable schedule — if a transaction Tj reads a data item previously written by a transaction Ti , then the commit operation of Ti appears before the commit operation of Tj.
The following schedule (Schedule 11) is not recoverable if T9 commits immediately after the read
If T8 should abort, T9 would have read (and possibly shown to the user) an inconsistent database state.
Cascading Rollbacks:
Cascading rollback – a single transaction failure leads to a series of transaction rollbacks. Consider the following schedule where none of the transactions has yet committed (so the schedule is recoverable)
Cascadeless Schedules:
Cascadeless schedules — cascading rollbacks cannot occur; 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.
It is desirable to restrict the schedules to those that are cascadeless, but doing so may reduce the amount of concurrency
Recall: Recoverable schedule — if a transaction Tj reads a data item previously written by a transaction Ti , then the commit operation of Tiappears before the commit operation of Tj.
Every cascadeless schedule is also recoverable
It is necessary to restrict the schedules to those that are recoverable
标签:Control,transaction,schedule,read,08,Transactions,database,transactions,conflict 来源: https://www.cnblogs.com/M1stF0rest/p/16410497.html