5th Unit Notes
5th Unit Notes
A Transaction
A Transaction consists of a series of read or write operation performed on a database. The
read or write operation performs a specific unit of work. A transaction is a collection of
read/write operations succeeding only if all contained operations succeed. However, a
transaction depends on two outcomes, success or failure. When a transaction starts
execution, it could be terminated by two possibilities, which are Abort or Commit. A
transaction is said to be aborted when it does not continue to a successful end or its not
successfully completed whereas a transaction is committed when execution is successfully
done and the changes stored accordingly. A database transaction must be atomic, consistent,
isolated and durable. This is often referred to as the ACID Properties.
Properties of a Transaction
There is need to ensure data integrity in a concurrent environment. The ACID properties help
to maintain the integrity of a database in terms of transactions. These properties describe the
major guarantees of a database transaction. There are four properties of a transaction. These
properties are represented by the acronym, ACID, which means Atomicity, Consistency,
Isolation and Durability.
Different States of a Transaction
There are different states of a transaction, which include active state, partially committed
state, committed state, failed and terminated states.
i. Active State: A transaction is said to be in the active state as soon as the execution
begins. Read/write operations can be performed at this stage.
ii. Partially Committed State: At the end of a transaction, it is partially committed.
iii. Committed State: A transaction is in a committed state when the execution has
been successfully completed, and changes stored or saved accordingly.
iv. Failed State: a transaction that is aborted while still in its active state is said to be
a failed transaction.
v. Terminated State: A transaction is said to be terminated when certain transactions
which are leaving the system cannot be continued or restarted.
NestedTransactions
A transaction that includes other transactions within its initiating point and an end point are
known as nested transactions. So, the nesting of the transactions is done in a transaction.
The nested transactions here are called sub-transactions.
The top-level transaction in a nested transaction can open sub-transactions, and each sub-
transaction can open more sub-transactions down to any depth of nesting.
A client’s transaction T opens up two sub-transactions, T1 and T2, which access objects on
servers X and Y, as shown in the diagram below.
T1.1, T1.2, T2.1, and T2.2, which access the objects on the servers M, N, and P, are opened
by the sub-transactions T1 and T2.
Nested Transaction
Concurrent Execution of the Sub-transactions is done which are at the same level – in the
nested transaction strategy. Here, in the above diagram, T1 and T2 invoke objects on
different servers and hence they can run in parallel and are therefore concurrent.
T1.1, T1.2, T2.1, and T2.2 are four sub-transactions. These sub-transactions can also run in
parallel.
Transaction T :
Start
Transfer Rs 105 from A to C :
Deduct Rs 105 from A(withdraw from A) & Add Rs 105 to C(deposit to
C)
Transfer Rs 205 from B to D :
Deduct Rs 205 from B (withdraw from B)& Add Rs 205 to D(deposit to
D)
End
Assuming:
1. Account A is on server X
2. Account B is on server Y,and
3. Accounts C and D are on server Z.
The transaction T involves four requests – 2 for deposits and 2 for withdrawals. Now they
can be treated as sub transactions (T1, T2, T3, T4) of the transaction T.
As shown in the figure below, transaction T is designed as a set of four nested transactions:
T1, T2, T3 and T4.
The problem with pessimistic concurrency control systems is that, if a transaction acquires
a lock on a resource so that no other transactions can access it. This will result in reducing
concurrency of the overall system.
The Optimistic Concurrency control techniques proceeds on the basis of assumption that, 0
or very less transactions will try to access a certain resource simultaneously. We can
describe a system as FULLY OPTIMISTIC, if it uses NO-Locks at all & checks for conflicts at
commit time. It has following 4-phases of operation:
• Read Phase: When a transaction begins, it read the data while also logging the
time-stamp at which data is read to verify for conflicts during the validation
phase.
• Execution Phase: In this phase, the transaction executes all its operation like
create, read, update or delete etc.
• Validation Phase: Before committing a transaction, a validation check is
performed to ensure consistency by checking the last_updated timestamp with
the one recorded at read_phase. If the timestamp matches, then the transaction
will be allowed to be committed and hence proceeds with commit phase.
• Commit phase: During this phase, the transactions will either be committed
or aborted, depending on the validation check performed during previous phase.
If the timestamp matches, then transactions are committed else they’re aborted.
Growing Phase: The transaction acquires all the locks on the data items that’ll be
required to execute the transaction successfully. No locks will be realease in this
phase.
Shrinking Phase: All the locks acquired in previous phase will be released one by one
and No New locks will be acquired in this phase.
The timestamp ordering algorithm works by assigning a unique timestamp to each transaction
when it arrives in the system. The timestamp reflects the transaction's start time, and it is
used to order the transactions for execution. The algorithm consists of two phases: the
validation phase and the execution phase.
• Validation Phase − The timestamp ordering method verifies each transaction's
timestamp during the validation stage to make sure the transactions are performed in
the proper sequence. When one transaction's timestamp is lower than another's, the
earlier transaction must be carried out.
• Execution Phase − In the execution phase, the timestamp ordering algorithm executes
the transactions in the order determined by the validation phase. If there is a conflict
between transactions, the algorithm uses a conflict resolution strategy to resolve the
conflict. One strategy is to abort the transaction with the lower timestamp, while
another strategy is to delay the transaction with the lower timestamp until the other
transaction completes.
Example: Let’s say we have two transaction T1 and T2, they operate on data item – A. The
Timestamp concurrency technique will keep track of the timestamp when the data was
accessed by transaction T1 first time.
Now, let’s say this transaction T1 is about to commit, before committing, it will check the
initial timestamp with the most recent timestamp. In our case, the transaction T1 won’t be
committed because a write operation by transaction T2 was performed.
if(Initial_timestamp == Most_recent_timestamp)
then ‘Commit’
else
‘Abort’
In our case, transaction will be aborted because T2 modified the same data item at 12:15PM
Flat transactions
• A flat transaction has a single initiating point (Begin) and a single end point
(Commit or abort). They are usually very simple and are generally used for short
activities rather than larger ones.
• A client makes requests to multiple servers in a flat transaction. Transaction T, for
example, is a flat transaction that performs operations on objects in servers X, Y,
and Z.
Before moving on to the next request, a flat client transaction completes the
previous one. As a result, each transaction visits the server object in order.
• A transaction can only wait for one object at a time when servers utilize locking.
Nested transactions
Refer above
Role of coordinator:
When the Distributed Transaction commits, the servers that are involved in the
transaction execution, for proper coordination, must be able to communicate with one
another.
When a client initiates a transaction, an “openTransaction” request is sent to any
coordinator server. The contacted coordinator carries out the “openTransaction” and
returns the transaction identifier to the client.
• Distributed transaction identifiers must be unique within the distributed system.
A simple way is to generate a TID contains two parts – the ‘server identifier”
(example: IP address) of the server that created it and a number unique to the
server.
The coordinator who initiated the transaction becomes the distributed
transaction’s coordinator and has the responsibility of either aborting it or
committing it.
• Every server that manages an object accessed by a transaction is a participant in
the transaction & provides an object we call the participant. The participants are
responsible for working together with the coordinator to complete the commit
process.
• The coordinator every time, records the new participant in the participants list.
Each participant knows the coordinator & the coordinator knows all the
participants. This enables them to collect the information that will be needed at
the time of commit and hence work in coordination.
Phase 1: Voting
• A “prepare message” is sent to each participating worker by the coordinator.
• The coordinator must wait until a response whether ready or not ready is
received from each worker, or a timeout occurs.
• Workers must wait until the coordinator sends the “prepare” message.
• If a transaction is ready to commit then a “ready” message is sent to the
coordinator.
• If a transaction is not ready to commit then a “no” message is sent to the
coordinator and resulting in aborting of the transaction.