Trasaction Management in DDBMS
Concurrency Control
CC in DDBMS Using Locks
Locks for concurrency control can be used in many different ways:
No Backups | Backups | |
---|---|---|
One Authority | One computer does all the locks. | There are backups if the primary fails. |
Many Authorities | Database items can have different computers in charge of their locks. | Many authorities with backups. |
There are various downsides to these methods:
- One Computer
- If it fails you need to restart the whole system.
- There are too many transactions requiring locks.
- Backup of One Computer
- Must be synced with the primary computer.
- Many Authorities
- Must be synced with the primary computer.
- Many Authorities with Backups
- Must be synced with the primary computer.
- Must be synced with the primary computer.
CC in DDBMS using Voting
- Each site with a copy of an item has a local lock that it can grant transaction for that item.
- If a transaction gets over half the local locks for an item, it has a global lock on the item.
- If so, it must tell the sites with a copy that it has the lock.
- If it takes too long, it must stop trying to get the lock.
This method is much more distributed but requires more communication.
Recovery in DDBMS
Violation of Atomicity
Even though atomicity is enforced at individual locations it can be violated globally.
This is because nodes can fail during the execution of a global transaction.
Two-Phase Commit Protocol
This is the protocol that is used to enforce atomicity in global transactions.
This is not related to 2PL.
There is a single coordinator on the network that decides when local transaction can commit.
Logging is handled at each node locally:
Messages sent and received from other nodes are logged too.
The two phases are as follows:
- Decide When to Commit or Abort
- Send
prepare T
to ask the nodes if they want to commit. - At each node decide whether to commit or abort:
-
If commit, go into precommitted state and send back
ready T
.Only the coordination can abort once a node is in a precommitted state.
-
If abort, send back
don't commit T
and abort local transaction.
-
- Send
- Commit or Abort
- If all nodes respond
ready T
:- Send
commit T
to all the nodes.
- Send
- If some node responds
don't commit T
or there is a timeout waiting for a reply:- Send
abort T
to all nodes.
- Send
- If all nodes respond
Two-Phase Logging
The logging for phase 1 is like so:
sequenceDiagram
participant T0 as T0 (Coordinator)
note over T0:log prepare T
T0 ->> T1: prepare T
alt ready
note over T1:flush T1 logs to enter precommitted state
note over T1:log ready T
T1 ->> T0:ready T
else abort
note over T1: log don't commit T
T1 ->> T0:don't commit T
note over T1:abort T1
end
alt
means that you can take either path in the sequence diagram.
The logging for phase 2 is like so:
sequenceDiagram
participant T0 as T0 (Coordinator)
alt all ready
note over T0: log commit T
T0 ->> T1:commit T
else don't commit/timeout
note over T0: log abort T
T0 ->> T1:abort T
end
Three-Phase Commit Protocol
In the two-phase commit protocol, if the coordinator and another transaction fails, durability could be broken when aborting the transaction.
To fix this we can split phase 2 into two parts:
- Prepare to Commit
- Send the decision (commit/abort) to all nodes.
- Nodes go into prepare-to-commit state.
- Commit
- The old phase 2.