Database_transactions
Database_transactions
10.1. Introduction
In our previous lesson we discussed physical database design. In our lesson
today, we will learn transaction management. Transaction management
deals with the problem of always keeping the database in a consistent state
even when concurrent accesses and failures occur. The important point is
that the database should be consistent when the transaction terminates.
10.2. Lesson objectives
By the end of this lesson, the learner will be able to:
Understand transaction basics: definition, ACID properties and
transaction states.
Understand the objective and tools of concurrency control
Understand failure types and the tools used in recovery management
Gain insight about transaction design issues.
Appreciate the role of transaction management in the larger area of
workflow management
10.3. Lesson outline
This lesson is structured as follows:
10.1. Introduction
10.2. Lesson objectives
10.3. Lesson outline
10.4. Transaction
10.5. Transaction properties
10.6. Transaction states
10.7. Transaction execution
10.8. Interference between concurrent transactions
10.9. Concurrency control
10.10. Lock based protocols
10.11. Types of locks
10. 12. Revision questions
10.13. Summary
10.14. Suggested reading
10.4. Transaction
A transaction is a series of operations carried out by a single user or
application program that must be treated as a logical unit of work. A
transaction transforms the database from one consistent state to another
consistent state.
Example
Begin
EXECSQL UPDATE Sales
SET Cost=Cost*1.2
WHERE FirstName=’James’
End
10.5. Transaction properties
To ensure the integrity of data the four transaction properties must be
fulfilled by any transaction.
[a]. Atomicity – This property ensures that for each transaction, either the
effects of all or none of its operations remain when a transaction is
completed (committed or aborted respectively). Transactions are never
left partially executed.
[b]. Consistency –This property requires that every transaction must leave
the database in a consistent (correct) state, i.e., maintain the
predetermined integrity rules of the database. A transaction must
transform a database from one consistent state to another consistent
state.
[c]. Isolation - Transactions cannot interfere with each other. Transactions
must behave as if they were executed in isolation.
[d]. Durability - Effects of successful (committed) transactions must persist
through crashes. Typically achieved by recording the transaction's effects
and its commit event in a non-volatile memory.
10.6. Transaction states
[i]. Active: This is the initial sate: the transaction is in this state when it is
executing.
[ii]. Partially committed: The state after the last statement has been
executed.
[iii]. Committed: The state after successful completion.
[iv]. Failed: State after discovery that a normal execution is no longer
possible. Can be as a result of logical error (e.g. bad input), system
error(e.g. deadlock) or system crash.
COMMIT
START TRANS END
TRANS
Partially Commit
committ ted
ed
Abort
Active
Fail
Aborte
d
ABORT ROLLBACK
Serial schedule
A schedule where new transaction is only executed after earlier transaction
has completed. Serial schedule is always consistent.
T1: Is consistent from start to finish.
T2: Is consistent from start to finish.
This is possible if T1 executes from start to finish before T2 or vise versa.
Serial schedule is slow. But it is possible to execute two or transactions
concurrently without interfering with each other.
Equivalent schedule
Schedule a and b are said to be equivalent iff;
T1…Tn operations appear in both schedules.
Operations that are conflicting appear in the same order.
Serializable schedule
A schedule sa said to be serializable if we are able to transform its
operations to another schedule sb so that the new schedule is equivalent to
original schedule .i.e. conflicting operations if any are in the same order and
sb is a serial schedule.
10.10. Conflicting operations
Two operations on the same data item conflict if at least one of the operations is a write.
Transactio Transactio Conflict?
nA nB
Read(x) Read(x) No
Write(x) Write(x) Yes
Read(x) Write(x) Yes
Write(x) Read(x) Yes
Rea(x) Read(y) No
Write(x) Write(y) No
Read(y) Write(x) No
Write(y) Read(x) No