Understanding Database Transactions and ACID Properties
Understanding Database Transactions and ACID Properties
T1
• Why we study transaction?
Read(A)
• According to general computation principle (operating system)
we may have partially executed program, as the level of A = A-100
atomicity is instruction i.e. either an instruction is executed Write(A)
completely or not
Read(B)
• But in DBMS view, user perform a logical work(operation)
which is always atomic in nature i.e. either operation is
B = B+100
execute or not executed, there is no concept like partial Write(B)
execution. For example, Transaction T1 which transfer 100
units from account A to B.
• READ (X) - Accessing the database item x from disk (where database stored data)
to memory variable also name as X.
• WRITE (X) - Writing the data item from memory variable X to disk.
T1
Read(A)
A = A-100
Write(A)
Read(B)
B = B+100
Write(B)
T1
Read(A)
A = A-100
Write(A)
Read(B)
B = B+100
Write(B)
• But interleaving of instructions between transactions may also lead to many problems
that can lead to inconsistent database.
• Sometimes it is possible that even though individual transaction are satisfying the acid
properties even though the final statues of the system will be inconsistent.
• So if somehow we proof that a non-serial schedule will also have same effects as of
a serial schedule than we can proof that, this particular non-serial schedule will
also be consistent.
Conflict
serializable
View
serializable
T1 T2 T1 T2 T1 T2 T1 T2
R(A) W(B) R(A) W(A)
W(B) R(A) W(A) R(A)
T1 T2 T1 T2 T1 T2 T1 T2
R(A) R(A) W(A) W(A)
R(A) R(A) W(A) W(A)
T1 T2 T1 T2
R(A) R(B)
A=A-50 B=B+50
R(B) R(A)
B=B+50 A=A-50
R(B) R(B)
B=B+50 B=B+50
R(A) R(A)
A=A+10 A=A+10
Knowledge Gate Website
SERIALIZABILITY
• Conflicting instructions - Let I and J be two consecutive instructions belonging to two different
transactions Ti and Tj in a schedule S, the possible I and J instruction can be as-
• I= READ(Q), J=READ(Q) ->Non-conflicting
• I= READ(Q), J=WRITE(Q) ->Conflicting
• I= WRITE(Q), J=READ(Q) ->Conflicting
• I= WRITE(Q), J=WRITE(Q) ->Conflicting
• So, the instructions I and J are said to be conflicting, if they are operations by different
transactions on the same data item, and at least one of these instructions is a write operation.
• If an edge Ti → Tj exists in the precedence graph, then in any serial schedule S’ equivalent to S,
Ti must appear before Tj.
• If the precedence graph for S has no cycle, then schedule S is conflict serializable, else it is
not.
• If a schedule is conflict serializable then it will also be view serializable, so we must check view
serializability only if a schedule is not conflict serializable.
• For each data item Q, if the transaction Ti reads the initial value of Q in schedule S , then then the
transaction Ti must, in schedule S’ ,also read the initial value of Q.
• If a transaction Ti in schedule S reads any data item Q, which is updated by transaction Tj, then a
transaction Ti must in schedule S’ also read data item Q updated by transaction Tj in schedule S’.
• For each data item Q, the transaction (if any) that performs the final write(Q) operation in schedule
S, then the same transaction must also perform final write(Q) in schedule S’.
Conflict
Recoverable
serializable
View
Cascadeless
serializable
Strict
S S
T1 T2 T1 T2
R(X) R(X)
W(X) W(X)
R(X) R(X)
C C
C C
Knowledge Gate Website
CASCADING ROLLBACK Vs CASCADELESS SCHEDULE
• It is a phenomenon, in which even if the schedule is recoverable, the a single transaction failure leads
to a series of transaction rollbacks, is called cascading rollback. Cascading rollback is undesirable, since
it leads to undoing of a significant amount of work. Uncommitted reads are not allowed in cascade less
schedule.
• A schedule in which for each pair of transactions Ti and Tj, such that if Tj reads a data item previously
written by Ti then the commit or abort of Ti must appear before read operation of Tj. Such a schedule
is called cascade less schedule.
S S
T1 T2 T3 T1 T2 T3
R(X) R(X)
W(X) W(X)
R(X) C
W(X) R(X)
R(X) W(X)
C C
C R(X)
C C
Knowledge Gate Website
Strict Schedule
• A schedule in which for each pair of transactions Ti and Tj, such that if Tj reads a data item
previously written by Ti then the commit or abort of Ti must appear before read and write
operation of Tj.
S1 S2 S3
T1 T2 T1 T2 T1 T2
R(a) R(a) R(a)
W(a) W(a) R(b)
W(a) C W(a)
C W(a) W(b)
R(a) R(a) C
C C R(a)
C
• Before any write operation modifies the database, a log record of that operation needs to be
created. This is to ensure that in case of a failure, the system can restore the database to a
consistent state using the log records.
Knowledge Gate Website
Aspect Deferred Database Modification Immediate Database Modification
Write Operation Timing Only at the commit point. As soon as changes occur.
In the shadow paging recovery technique, the database maintains two page tables during a
transaction: the current page table (reflecting the state before the transaction began) and the
shadow page table (which tracks changes made during the transaction). Here's how it operates:
• Initialization: When a transaction begins, the database creates a shadow copy of the page
table. The database pages themselves are not duplicated; only the page table entries are
duplicated.
• Modifications: As the transaction progresses, any changes are reflected in the current page
table, while the shadow page table retains the original state. If a page is modified, a new copy
of the page is created, and the current page table is updated to point to this new version,
thereby ensuring that the shadow page table still points to the original unmodified page.
• Recovery: In case of a system failure before the transaction commits, the database can easily
recover by discarding the current page table and reverting back to the shadow page table,
thereby restoring the database to its state before the transaction began.
• Data Replication
• Advantages: Increased Availability, Improved Performance, Enhanced Reliability
• Disadvantages: Storage Costs, Maintenance Complexity, Write Complexity.
• Data Fragmentation
• Advantages: Efficient Data Access, Distributed Processing, Localized Management.
• Disadvantages: Complexity, Dependency on Network, Reconstruction Issues.
• Lock based method: - where we ask a transaction to first lock a data item before using it. So
that no different transaction can use a data at the same time, removing any possibility of
conflict.
• 2 phase locking
1. Basic 2pl
2. Conservative 2pl
3. Rigorous 2pl
4. Strict 2pl
• Graph based protocol
• Validation based protocol – Majority of transactions are read only transactions, the rate of
conflicts among the transaction may be low, thus many of transaction, if executed without the
supervision of a concurrency control scheme, would nerveless leave the system in a consistent
state. Knowledge Gate Website
TIME STAMP ORDERING PROTOCOL
• Basic idea of time stamping is to decide the order between the transaction
before they enter in the system using a stamp (time stamp), in case of any
conflict during the execution order can be decided using the time stamp.
• Let’s understand how this protocol works, here we have two idea of
timestamping, one for the transaction, and other for the data item.
• With each transaction ti, in the system, we associate a unique fixed timestamp, denoted by
TS(ti).
• If a transaction has been assigned a timestamp TS(ti) and a new transaction tj , enters into
the system with a timestamp TS(tj), then always TS(ti) <TS(tj).
2. Second it is unique means no two transaction can have the same timestamp.
• These timestamps are updated whenever a new read(Q) or write(Q) instruction is executed.
1. If TS(Ti ) < W-timestamp(Q), then Ti needs to read a value of Q that was already
overwritten. Hence, the read operation is rejected, and Ti is rolled back.
2. If TS(Ti ) < W-timestamp(Q), then Ti is attempting to write an obsolete value of Q. Hence, this
write operation is rejected, and Ti is rolled back.
3. If TS(Ti ) ≥ R-timestamp(Q), then the write operation is executed, and W-timestamp(Q) is set to
max(W-timestamp(Q), TS(Ti )).
4. If TS(Ti ) ≥ W-timestamp(Q), then the write operation is executed, and W-timestamp(Q) is set to
max(W-timestamp(Q), TS(Ti )).
• The protocol rules for read operations remain unchanged. while for write operation, there is
slightly change in Thomas write rule than timestamp ordering protocol.
• Thomas' Write Rule allows greater potential concurrency. Allows some view-serializable
schedules that are not conflict serializable.
• Lock based protocols ensure this requirement. Idea is first obtain a lock on the
desired data item then if lock is granted then perform the operation and then
unlock it.
• Shared mode
• If transaction Ti has obtained a shared-mode lock (denoted by S) on any data item
Q, then Ti can read, but cannot write Q, any other transaction can also acquire a
shared mode lock on the same data item(this is the reason we called this shared
mode).
• Exclusive mode
• If transaction Ti has obtained an exclusive-mode lock (denoted by X) on any data
item Q, then Ti can both read and write Q, any other transaction cannot acquire
either a shared or exclusive mode lock on the same data item. (this is the reason we
called this exclusive mode)
• We shall require that each transaction in the system follow a set of rules, called a locking
protocol, indicating when a transaction may lock and unlock each of the data items for e.g. 2pl
or graph based locking.
• Growing phase- A transaction may obtain locks, but not release any locks.
• Shrinking phase- A transaction may release locks, but may not obtain any new locks.
T1 T2
LOCK-X(A)
READ(A)
WRITE(A)
LOCK-S(B)
READ(B)
LOCK-X(B)
READ(B)
WRITE(B)
LOCK-S(A)
READ(A)
UNLOCK(B)
UNLOCK(A)
UNLOCK(B)
Knowledge Gate Website
UNLOCK(A)
Conflict View Recoverability Cascadelessness Deadlock
Serializability Serializability Freedom
Rigorous 2PL
Strict 2PL
• If a schedule is allowed in 2PL protocol then definitely it is always conflict serializable. But it is
not necessary that if a schedule is conflict serializable then it will be generated by 2pl.
Equivalent serial schedule is based on the order of lock points.
• Shrinking phase will work as usual, and transaction can unlock any data item
anytime.
• This protocol requires that locking be two phase and also all the locks taken be
held by transaction until that transaction commit.
• This protocol requires that locking be two phase and also that exclusive –mode locks taken by
transaction be held until that transaction commits.
IS IX S X
IS
IX
S
X
IS IX S X
IS true true true false
IX true true false false
S true false true false
X false false false false
• A difficulty in reducing the overhead is that we do not know in advance which transactions
will be involved in a conflict. To gain that knowledge, we need a scheme for monitoring the
system.
• The validation protocol requires that each transaction Ti executes in two or three different
phases in its lifetime, depending on whether it is a read-only or an update transaction. The
phases are, in order:
Knowledge Gate Website
• Read phase. During this phase, the system executes transaction Ti. It reads the values of the
various data items and stores them in variables local to Ti. It performs all write operations on
temporary local variables, without updates of the actual database.
• Validation phase. The validation test (described below) is applied to trans- action Ti. This
determines whether Ti is allowed to proceed to the write phase without causing a violation of
serializability. If a transaction fails the validation test, the system aborts the transaction.
• Write phase. If the validation test succeeds for transaction Ti, the temporary local variables
that hold the results of any write operations performed by Ti are copied to the database.
Read-only transactions omit this phase.