Вы находитесь на странице: 1из 22

Distributed Transactions

Concurrency Control & Recovery

Outline
Transaction Management Overview ACID Properties of Transactions Transaction State

Distributed Transactions
Distributed Concurrency Control

Lock Management Phantom deadlocks

Distributed deadlocks

Distributed Recovery Commit Protocols Two phase commit (2PC) Three-Phase Commit (3PC)

Transaction Management Overview


A transaction is a unit of program execution that

accesses and possibly updates various data items. E.g. transaction to transfer Rs.50 from account A to account B:
1. 2. 3. 4. 5. 6. read_from_account(A) A := A 50 write_to_account(A) read_from_account(B) B := B + 50 write_to_account(B)

Two main issues to deal with:


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

ACID Properties of Transactions


Atomicity: Either all actions are carried out, or

none are Consistency: If each transaction is consistent, and the database is initially consistent, then it is left consistent Isolation: Transactions are isolated, or protected, from the effects of other scheduled transactions Durability: If a transactions completes successfully, then its effects persist
Qu. Define ACID properties of a database.

Atomicity And Consistency


Atomicity: An executable program, assumed that this program will finally terminate, has one initial state and one final state. If the program achieves its final state it is said to be committed, otherwise if it is at the initial state after some execution steps then it is aborted or rollback. Consistency: If a program produces consistent result only then it satisfies the consistency property and it will be at the final state or committed. If the result is not consistent then a transaction program should be at the initial state, in other word the transaction is aborted.

Isolation And Durability


Isolation: if a program is executing and if it is only single program on the system then it satisfies the isolation property. If there are several other processes on the system, then none of the intermediate state of this program is viewable until it reaches its final state Durability: If a program reaches to its final state and the result is made available to the outside world then this result is made permanent. Even a system failure cannot change this result. In other words, when a transaction commits its state is durable.

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.

Transaction State

Lost updates
Consider two transactions A

and B which add 10 and 20 respectively to a value V A and B both take a copy of the original value of V They both change the value in memory A puts back its new value first and then B puts back its new value which immediately overwrites As change A's update is lost!

A B | | Get V | | Get V Add 10 | | Add 20 Put V | put V

V 5 5 5 5 5 15 25

Temporary Update
A updates V B uses A's updated value A aborts and V's old value

is restored B continues with erroneous value!

A B | | Store 20 in V | | Get V Crash & | Rollback | Use wrong value of V |

V 5 20 20
5 5 5

Incorrect Summary
A updates all the values in

a set V B calculates an average while A is half-way through B uses inconsistent data


One solution to these

problems is to use locks

B A | | | Update V1 | Update V2 Read all Vs . | . Calculate . avg . | . Update Vn

Distributed Transactions
In the distributed DBMS, a given

transaction is submitted at some one site, but it can access data at other sites as well. The activity of transaction at a given site as a subtransaction. Distributed transaction must have all four ACID properties. Example: A transaction to book a trip, which consist of booking a flight, a rental car and a hotel.
Qu. Illustrate with example Distributed transaction? Qu. Write a note on Distributed transaction.? OR

Distributed Concurrency Control


Distributed concurrency control is the concurrency control

of a system distributed over a computer network. In database system and transaction management distributed concurrency control refers to the concurrency control of a distributed database. It also refers to the concurrency control in multidatabase environment. E.g. federated database, grid computing and cloud computing environments. The major goal for distributed concurrency control is distributed serializability.

Lock Management
Lock management can be distributed across sites in many ways:

Centralized: A single site is in charge of handling lock and

unlock requests for all objects. Primary copy: One copy of each object is designated as the primary copy. All requests to lock or unlock a copy of this object are handled by the lock manager at the site where the primary copy is stored, regardless of where the copy itself is stored. Fully distributed: Requests to lock or unlock a copy of an object stored at a site are handled by the lock manager at the site where the copy is stored.
Qu. What are the choices for managing locks in distributed databases? Qu. Explain how locks can be distributed across diff sites in Concurrency Control of Distributed DB?

Distributed deadlocks
Distributed deadlocks

A cycle in the global wait-for graph An example Simple resolution A centralized deadlock detector collect latest copy of each servers local wait-for graph construct global wait-for graph find cycles in the global wait-for graph Drawbacks poor availability, lack of fault tolerance, poor scalability cost of collecting information is high

Phantom deadlocks
Phantom deadlocks

a deadlock that is detected but is not really a deadlock

may occur when some deadlocked transactions abort or release locks

An example

at server Y: U request lock V at server X: U release lock for T at global deadlock detector: message from server Y arrives earlier than message from server X, then phantom deadlock happens

Qu. Explain the dead lock in distributed database?

Distributed Recovery
Recovery

Restoring the server with the latest committed versions of its objects from permanent storage The task of the recovery manager Save objects in permanent storage (in a recovery file) for committed transactions Restore the servers objects after a crash Reorganize the recovery file to improve the performance of recovery Reclaim storage space ( in the recovery file)

Commit Protocols

How to execute commit for distributed transactions Issue: How to ensure atomicity and durability One-phase commit (1PC): the coordinator communicates with all servers to commit. Problem: a server can not abort a transaction. Two-phase commit (2PC): allow any server to abort its part of a transaction. Commonly used. Three-phase commit (3PC): avoid blocking servers in the presence of coordinator failure. Mostly referred in literature, not in practice.

Qu. What are commit protocol? Why is it require in a distributed database?

Two phase commit (2PC)

Consider a distributed transaction involving the participation of a number of processes each running on a different machine, and assuming no failure occur Phase1: The coordinator gets the participants ready to write the results into the database Phase2: Everybody writes the results into database
Coordinator: The process at the site where the transaction

originates and which controls the execution Participant: The process at the other sites that participate in executing the transaction

Global commit rule:


The coordinator aborts iff at least one participant votes to abort The coordinator commits iff all the participants vote to commit

Qu. Explain how two-phase commit protocol works to commit distributed transaction?

Three-Phase Commit (3PC)


A commit protocol called Three-Phase Commit (3PC) can

avoid blocking even if the coordinator site fails during recovery. The basic idea is that when the coordinator sends out prepare messages and receives yes votes from all subordinates, it sends all sites a precommit message, rather than a commit message. In 3PC the coordinator effectively postpones the decision to commit until it is sure that enough sites know about the decision to commit; if the coordinator subsequently fails, these sites can communicate with each other and detect that the transaction must be committed
Qu. Describe three-phase commit protocol?

References
21

Database Management Systems by Ramakrishnan www.wikipedia.com

Вам также может понравиться