Transaction Processing Concepts 1 Introduction To transaction Processing

  • Slides: 36
Download presentation
Transaction Processing Concepts

Transaction Processing Concepts

1. Introduction To transaction Processing • 1. 1 Single User VS Multi User Systems

1. Introduction To transaction Processing • 1. 1 Single User VS Multi User Systems – One criteria to classify Database is according to number of user that concurrently connect to the system. • Single User: only one user use the system in each time • Multi User: many users use the system in the same time

What is a Transaction? • A transaction is a unit of program execution that

What is a Transaction? • A transaction is a unit of program execution that accesses and possibly updates various data items. • A transaction must see a consistent database. • During transaction execution the database may be inconsistent. • When the transaction is committed, the database must be consistent.

– Transaction is an executing program that forms a logical unit of database processing.

– Transaction is an executing program that forms a logical unit of database processing. – A transaction include one or more database access operations. – The database operation can be embedded within an application or can be specified by high level query language. – Specified boundary by Begin and End transaction statements – If the database operations in a transaction do not update the database, it is called “Read-only transaction”

Example of transaction • Let Ti be a transaction that transfer money from account

Example of transaction • Let Ti be a transaction that transfer money from account A (5000) to account B. The transaction can be defined as – Ti: read (A) A : = A – 5000 write (A); read (B) B : = B + 5000 write (B) (withdraw from A) (update A) (deposit B) (update B)

Example Of Transfer • Transaction to transfer 5000 from Checking account A to Saving

Example Of Transfer • Transaction to transfer 5000 from Checking account A to Saving account B: 1. read(A) 2. A : = A – 5000 3. write(A) 4. read(B) 5. B : = B + 5000 6. write(B) • Consistency requirement – the sum of A and B is unchanged by the execution of the transaction. • Atomicity requirement — if the transaction fails after step 3 and before step 6, the system should ensure that its updates are not reflected in the database, else an inconsistency will result.

Transfer Example (Cont. ) • Durability requirement — once the user has been notified

Transfer Example (Cont. ) • Durability requirement — once the user has been notified that the transaction has completed (i. e. , the transfer of the 5000 has taken place), the updates to the database by the transaction must persist despite failures. • Isolation requirement — if between steps 3 and 6, another transaction is allowed to access the partially updated database, it will see an inconsistent database.

Desirable properties of transaction : ACID properties • To ensure integrity of data, we

Desirable properties of transaction : ACID properties • To ensure integrity of data, we require that the database system maintain the following properties of the transactions: – Atomicity. – Consistency preservation. – Isolation. – Durability or permanency.

ACID • Atomicity. Either all operations of the transaction are reflected properly in the

ACID • Atomicity. Either all operations of the transaction are reflected properly in the database, or none are. • Consistency. Execution of a transaction in isolations (that is, with no other transaction executing concurrently) • Isolation. Even though multiple transactions may execute concurrently, the system guarantees that, for every pair of transactions Ti and Tj, it appears to Ti that – either Tj finished execution before Ti started, – or Tj started execution after Ti finished. – Thus, each transaction is unaware of other transactions executing concurrently in the system. (Execution of transaction should not be interfered with by any other transactions executing concurrently) • Durability. After a transaction completes successfully, the changes it has made to the database persist, even if there are system failures. (The changes must not be lost because of any failure)

Consistency • Consistency. – The consistency requirement here is that the sum of A

Consistency • Consistency. – The consistency requirement here is that the sum of A and B be unchanged by the execution of the transaction. – Without consistency requirement, money could be created or destroyed by the transaction. – It can be verified, • If the database is consistency before an execution of the transaction, the database remains consistent after the execution of the transaction.

Atomicity • Atomicity. Either all operations of the transaction are reflected properly in the

Atomicity • Atomicity. Either all operations of the transaction are reflected properly in the database, or none are. – State before the execution of transaction Ti • The value of A = 50, 000 • The value of B = 100 – Failure occur (ex. Hardware failure) • • Failure happen after the WRITE(A) operation (at this moment A = 50000 – 5000 = 45000) And the value of B = 100 (inconsistency state) In consistency state A = 45000 and B = (5100)

(cont. ) – Idea behind ensuring atomicity is following: • The database system keeps

(cont. ) – Idea behind ensuring atomicity is following: • The database system keeps track of the old values of any data on which a transaction performs a write • If the transaction does not complete, the DBMS restores the old values to make it appear as though the transaction have never execute.

Durability or permanency • Durability or permanency. After a transaction completes successfully, the changes

Durability or permanency • Durability or permanency. After a transaction completes successfully, the changes it has made to the database persist, even if there are system failures. • These changes must not be lost because of any failure • ensures that, transaction has been committed, that transaction’s updates do not get lost, even if there is a system failure

Isolation • Isolation. Even though multiple transactions may execute concurrently, the system guarantees that,

Isolation • Isolation. Even though multiple transactions may execute concurrently, the system guarantees that, • • • for every pair of transactions Ti and Tj, it appears to Ti that either Tj finished execution before Ti started, or Tj started execution after Ti finished. Thus, each transaction is unaware of other transactions executing concurrently in the system. ( Execution of transaction should not be interfered with by any other transactions executing concurrently )

State of transaction • Active, the initial state; the transaction stays in this state

State of transaction • 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 backed and the database has been restored to its state prior to the start of transaction. • Committed, after successful completion

State diagram of a transition Read Write Begin Transaction Partially Committed Commit committed Abort

State diagram of a transition Read Write Begin Transaction Partially Committed Commit committed Abort Active Abo rt Failed A transaction must be in one of these states. Aborted (Terminate)

 • The transaction has committed only if it has entered the committed state.

• The transaction has committed only if it has entered the committed state. • The transaction has aborted only if it has entered the aborted state. • The transaction is said to have terminated if has either committed or aborted.

Concurrency Control

Concurrency Control

Concurrent Executions • Transaction processing permit – Multiple transactions to run concurrently. – Multiple

Concurrent Executions • Transaction processing permit – Multiple transactions to run concurrently. – Multiple transactions to update data concurrently • Cause – Complications with consistency of data

Reason for allowing concurrency • Improved throughput of transactions and system resource utilization •

Reason for allowing concurrency • Improved throughput of transactions and system resource utilization • Reduced waiting time of transactions

Concurrent Executions • Multiple transactions are allowed to run concurrently in the system. Advantages

Concurrent Executions • Multiple transactions are allowed to run concurrently in the system. Advantages are: – increased processor and disk utilization, leading to better transaction throughput: one transaction can be using the CPU while another is reading from or writing to the disk – reduced waiting time for transactions: short transactions need not wait behind long ones.

Possible Problems • Lost update problem • Temporary update problem • Incorrect summary problem

Possible Problems • Lost update problem • Temporary update problem • Incorrect summary problem

A = 1000, B =2000 Lost update problem T 1 Read_item(A) A = 1000

A = 1000, B =2000 Lost update problem T 1 Read_item(A) A = 1000 A : = A – 50 A = 950 Write_item(A) A = 950 Read_item(B) B = 2000 T 2 A = 950 Read_item(A) temp = 95 temp : = 0. 1*A A: = A-temp A=950 -95 = 855 Write_item(A) Read_item(B) A = 855 B = 2000 B = 2050 B : = B + 50 Write_item(B) B = 2050 B : = B + temp B = 2095 Write_item(B) B = 2095

Temporary update problem R = 3000 T 1 T 2 Read_item(R) - Write_item(R) R

Temporary update problem R = 3000 T 1 T 2 Read_item(R) - Write_item(R) R = 1000 Roll. Back R = 3000

Inconsistency problem A = 40 , B = 50, C = 30 T 1

Inconsistency problem A = 40 , B = 50, C = 30 T 1 A = 40 Read_item(A) Sum = 40 SUM = Sum+A Read_item(B) B = 50 SUM = A + B SUM = 40+50 = 90 After A+B+C = 50+50+20 = 120 A = 40+10 =50 T 2 A+B+C = 40+50+30 = 120 Read_item(C) C = 30 C = C - 10 C = 30 -10 =20 Write_item(C) C = 20 Read_item(A) A = 40 A = A + 10 Write_item(A) A = 50 COMMIT Read_item(C) C = 20 SUM = SUM + C Sum = 90 + 20 = 110

Schedules • Schedules – sequences that indicate the chronological order in which instructions of

Schedules • Schedules – sequences that indicate 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.

Example Schedules • Let T 1 transfer $50 from A to B, and T

Example Schedules • Let T 1 transfer $50 from A to B, and T 2 transfer 10% of the balance from A to B. The following is a serial schedule (Schedule 1 in the text), in which T 1 is followed by T 2.

Cont. • Let T 1 and T 2 be the transactions defined previously. The

Cont. • Let T 1 and T 2 be the transactions defined previously. The following schedule is not a serial schedule, but it is equivalent to Schedule 1.

Cont. • The following concurrent schedule does not preserve the value of the sum

Cont. • The following concurrent schedule does not preserve the value of the sum A + B.

Serializability • Basic Assumption – Each transaction preserves database consistency. • Thus serial execution

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

Conflict Serializability • Instructions li and lj of transactions Ti and Tj respectively, conflict

Conflict Serializability • 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

Conflict Serializability (Cont. ) • If a schedule S can be transformed into a

Conflict Serializability (Cont. ) • 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 a schedule that is not conflict serializable: T 3 T 4 read(Q) write(Q) We are unable to swap instructions in the above schedule to obtain either the serial schedule < T 3, T 4 >, or the serial schedule < T 4, T 3 >.

Conflict Serializability (Cont. ) • Schedule 3 below can be transformed into Schedule 1,

Conflict Serializability (Cont. ) • Schedule 3 below can be transformed into Schedule 1, a serial schedule where T 2 follows T 1, by series of swaps of non-conflicting instructions. Therefore Schedule 3 is conflict serializable.

View Serializability • Let S and S´ be two schedules with the same set

View Serializability • 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: 1. For each data item Q, if transaction Ti reads the initial value of Q in schedule S, then transaction Ti must, in schedule S´, also read the initial value of Q. 2. For each data item Q if transaction Ti executes read(Q) in schedule S, and that value was produced by transaction Tj (if any), then transaction Ti must in schedule S´ also read the value of Q that was produced by transaction Tj. 3. For each data item Q, the transaction (if any) that performs the final write(Q) operation in schedule S must perform the final write(Q) operation in schedule S´. As can be seen, view equivalence is also based purely on reads and writes alone.

View Serializability (Cont. ) • A schedule S is view serializable it is view

View Serializability (Cont. ) • A schedule S is view serializable it is view equivalent to a serial schedule. • Every conflict serializable schedule is also view serializable. • Schedule 9 (from text) — a schedule which is view-serializable but not conflict serializable. Every view serializable schedule that is not conflict serializable has blind writes.