Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
95 views15 pages

CO 3 Transaction

The document discusses transactions in databases. A transaction is a set of related operations performed as one logical unit. Transactions have ACID properties - atomicity, consistency, isolation, and durability - to maintain data integrity when multiple transactions occur concurrently. Concurrency issues like lost updates, dirty reads, and non-repeatable reads can arise if transactions are not properly isolated from each other.

Uploaded by

Hameed Basha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views15 pages

CO 3 Transaction

The document discusses transactions in databases. A transaction is a set of related operations performed as one logical unit. Transactions have ACID properties - atomicity, consistency, isolation, and durability - to maintain data integrity when multiple transactions occur concurrently. Concurrency issues like lost updates, dirty reads, and non-repeatable reads can arise if transactions are not properly isolated from each other.

Uploaded by

Hameed Basha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Transaction:

o The transaction is a set of logically related operation. It


contains a group of tasks.
o A transaction is an action or series of actions. It is
performed by a single user to perform operations for
accessing the contents of the database.

Example: Suppose an employee of bank transfers Rs 800


from X's account to Y's account. This small transaction
contains several low-level tasks:

X's Account:

1. Open_Account(X)
2. Old_Balance = X.balance
3. New_Balance = Old_Balance - 800
4. X.balance = New_Balance
5. Close_Account(X)

Y's Account

1. Open_Account(Y)
2. Old_Balance = Y.balance
3. New_Balance = Old_Balance + 800
4. Y.balance = New_Balance
5. Close_Account(Y)
Operations of Transaction:

Following are the main operations of transaction:

Read(X): Read operation is used to read the value of X


from the database and stores it in a buffer in main memory.

Write(X): Write operation is used to write the value back


to the database from the buffer.

Let's take an example to debit transaction from an account


which consists of following operations:

1. R(X);
2. X = X - 500;
3. W(X);

Let's assume the value of X before starting of the


transaction is 4000.

o The first operation reads X's value from database and


stores it in a buffer.
o The second operation will decrease the value of X by
500. So buffer will contain 3500.
o The third operation will write the buffer's value to
the database. So X's final value will be 3500.

But it may be possible that because of the failure of


hardware, software or power, etc. that transaction may fail
before finished all the operations in the set.
For example: If in the above transaction, the debit
transaction fails after executing operation 2 then X's value
will remain 4000 in the database which is not acceptable by
the bank.

To solve this problem, we have two important operations:

Commit: It is used to save the work done permanently.

Rollback: It is used to undo the work done.


Transaction property:
The transaction has the four properties. These are used to
maintain consistency in a database, before and after the
transaction.
Property of Transaction (ACID)
1. Atomicity
2. Consistency
3. Isolation
4. Durability
Atomicity:
o It states that all operations of the transaction take place at
once if not, the transaction is aborted.
o There is no midway, i.e., the transaction cannot occur
partially. Each transaction is treated as one unit and either
run to completion or is not executed at all.
Atomicity involves the following two operations:
Abort: If a transaction aborts then all the changes made are not
visible.
Commit: If a transaction commits then all the changes made are
visible.
Example: Let's assume that following transaction T consisting of
T1 and T2. A consists of Rs 600 and B consists of Rs 300. Transfer
Rs 100 from account A to account B.

T1 T2

Read(A) Read(B)
A:= A-100 Y:= Y+100
Write(A) Write(B)

After completion of the transaction, A consists of Rs 500 and B


consists of Rs 400.
Inconsistent database state:
If the transaction T fails after the completion of transaction T1
but before completion of transaction T2, then the amount will
be deducted from A but not added to B. This shows the
inconsistent database state. In order to ensure correctness of
database state, the transaction must be executed in entirety.
Consistency:
o The integrity constraints are maintained so that the
database is consistent before and after the transaction.
o The execution of a transaction will leave a database in
either its prior stable state or a new stable state.
o The consistent property of database states that every
transaction sees a consistent database instance.
o The transaction is used to transform the database from one
consistent state to another consistent state.
For example: The total amount must be maintained before or
after the transaction.
1. Total before T occurs = 600+300=900
2. Total after T occurs= 500+400=900
Therefore, the database is consistent. In the case when T1 is
completed but T2 fails, then inconsistency will occur.
Isolation
 It shows that the data which is used at the time of
execution of a transaction cannot be used by the second
transaction until the first one is completed.
 In isolation, if the transaction T1 is being executed and
using the data item X, then that data item can't be accessed
by any other transaction T2 until the transaction T1 ends.
 The concurrency control subsystem of the DBMS enforced
the isolation property.
Durability:
 The durability property is used to indicate the performance
of the database's consistent state. It states that the
transaction made the permanent changes.
 They cannot be lost by the erroneous operation of a faulty
transaction or by the system failure. When a transaction is
completed, then the database reaches a state known as the
consistent state. That consistent state cannot be lost, even
in the event of a system's failure.
 The recovery subsystem of the DBMS has the responsibility
of Durability property.
Concurrent Execution of Transaction:
In the transaction process, a system usually allows executing
more than one transaction simultaneously. This process is called
a concurrent execution.
Advantages of concurrent execution of a transaction:
1. Decrease waiting time or turnaround time.
2. Improve response time
3. Increased throughput or resource utilization.

Problems with Concurrent Execution/ Transaction:


Several problems can occur when concurrent transactions are
run in an uncontrolled manner such type of problems is known
as concurrency problems.
There are following different types of problems or conflicts
which occur due to concurrent execution of transaction:
Problem 1. Lost update problem (Write – Write conflict)
This type of problem occurs when two transactions in database
access the same data item and have their operations in an
interleaved manner that makes the value of some database
item incorrect.
If there are two transactions T1 and T2 accessing the same data
item value and then update it, then the second record
overwrites the first record.
Example: Let's take the value of A is 100

Time Transaction T1 Transaction T2

t1 Read(A)

t2 A=A-50

t3 Read(A)

t4 A=A+50

t5 Write(A)

t6 Write(A)

Here,
 At t1 time, T1 transaction reads the value of A i.e., 100.
 At t2 time, T1 transaction deducts the value of A by 50.
 At t3 time, T2 transactions read the value of A i.e., 100.
 At t4 time, T2 transaction adds the value of A by 150.
 At t5 time, T1 transaction writes the value of A data item on
the basis of value seen at time t2 i.e., 50.
 At t6 time, T2 transaction writes the value of A based on
value seen at time t4 i.e., 150.
 So at time T6, the update of Transaction T1 is lost because
Transaction T2 overwrites the value of A without looking at
its current value.
 Such type of problem is known as the Lost Update Problem.
Problem 2. Dirty read problem (W-R conflict)
This type of problem occurs when one transaction T1 updates a
data item of the database, and then that transaction fails due to
some reason, but its updates are accessed by some other
transaction.
Example: Let's take the value of A is 100

Time Transaction T1 Transaction T2

t1 Read(A)

t2 A=A+20

t3 Write(A)

t4 Read(A)
t5 A=A+30

t6 Write(A)

t7 Server down/ROLLBACK

Here,
 At t1 time, T1 transaction reads the value of A i.e., 100.
 At t2 time, T1 transaction adds the value of A by 20.
 At t3 time, T1transaction writes the value of A (120) in the
database.
 At t4 time, T2 transactions read the value of A data item i.e., 120.
 At t5 time, T2 transaction adds the value of A data item by 30.
 At t6 time, T2 transaction writes the value of A (150) in the
database.

 At t7 time, a T1 transaction fails due to power failure then it


is rollback according to atomicity property of transaction
(either all or none).
 So, transaction T2 at t4 time contains a value which has not
been committed in the database. The value read by the
transaction T2 is known as a dirty read.
Problem 3. Unrepeatable read (R-W Conflict)
It is also known as an inconsistent retrieval problem. If a
transaction T1 reads a value of data item twice and the data item
is changed by another transaction T2 in between the two read
operation. Hence T1 access two different values for its two read
operation of the same data item.
Example: Let's take the value of A is 100

Time Transaction T1 Transaction T2

t1 Read(A)

t2 Read(A)

t3 A=A+30

t4 Write(A)

t5 Read(A)

Here,
 At t1 time, T1 transaction reads the value of A i.e., 100.
 At t2 time, T2transaction reads the value of A i.e., 100.
 At t3 time, T2 transaction adds the value of A data item by 30.
 At t4 time, T2 transaction writes the value of A (130) in the
database.
 Transaction T2 updates the value of A. Thus, when another
read statement is performed by transaction T1, it accesses
the new value of A, which was updated by T2. Such type of
conflict is known as R-W conflict.
Question: Why do you need concurrency in Transactions?

A database is a shared resource accessed. It is used by many users and processes concurrently. For
example, the banking system, railway, and air reservations systems, stock market monitoring, supermarket
inventory, and checkouts, etc.

Not managing concurrent access may create issues like:

 Hardware failure and system crashes

 Concurrent execution of the same transaction, deadlock, or slow performance

States of Transactions:

State Transaction types


The active state is the first state of every transaction. In this state, the transaction is being
executed.
Active State
For example: Insertion or deletion or updating a record is done here. But all the records
are still not saved to the database.

In the partially committed state, a transaction executes its final operation, but the data is
Partially still not saved to the database.
Committed In the total mark calculation example, a final display of the total marks step is executed in
this state.

Committed A transaction is said to be in a committed state if it executes all its operations successfully.
State In this state, all the effects are now permanently saved on the database system.

If any of the checks made by the database recovery system fails, then the transaction is
said to be in the failed state.
Failed State
In the example of total mark calculation, if the database is not able to fire a query to fetch
the marks, then the transaction will fail to execute.

If any of the checks fail and the transaction has reached a failed state then the database
recovery system will make sure that the database is in its previous consistent state. If not
then it will abort or roll back the transaction to bring the database into a consistent state.

If the transaction fails in the middle of the transaction then before executing the
Terminated transaction, all the executed transactions are rolled back to its consistent state.
State
After aborting the transaction, the database recovery module will select one of the two
operations:

 Re-start the transaction


 Kill the transaction

Example:
Let’s suppose a transaction T through which you want to
transfer 100 Rs from account ‘X’ (source account) to account ‘Y’
(destination account). Assume that the account balance of X is
500 and Y is 1000. This transaction T can be represented as:

Transaction T
BEGIN TRANSACTION

Read(X); …..(1)

X=X-100; …..(2)

Write(X); …..(3)

Read(Y); …..(4)

Y=Y+100; …..(5)

Write(Y); …..(6)

Commit; …..(7)

END TRANSACTION
ACTIVE: When the above transaction T starts, i.e., enters BEGIN
TRANSACTION, the transaction in an active state. From BEGIN
TRANSACTION and COMMIT statement, the transaction is in the
ACTIVE state only.
In an ACTIVE state, value of X= 500 and value of Y = 1000.
PARTIALLY COMMITTED: If T transaction reaches the
statement COMMIT, it goes into partially committed state.
In PARTIALLY COMMITTED state, X= 400 and Y= 1100;
FAILED: This state happens if one of the following occurs:
If any system or database failure happens to the transaction in
an ACTIVE state, then the transaction goes into an FAILED state.
 If the transaction failed before Write(X), then X = 500 and Y = 1000.
 If the transaction failed after Write(X), then X= 400 and Y = 1000.
 If the transaction failed before Commit and after WRITE(Y)
statement, then X = 400 and Y = 1100.
 If any failures happen to the above transaction T in a partially
committed state, then the transaction goes into the failed state, the
value of X is 500, and Y is 1000.

ABORTED: In an aborted state, X= 500 and Y= 1000. [X and Y


both are rolled back to Old consistent state]
COMMITTED: If above transaction T executes COMMIT
statement successfully. In other words, if it successfully writes
the new value of X and Y into the database or a log file, then the
transaction is said to be in a committed state.
In COMMITTED state, X = 400 and Y = 1100. [New consistent state]

You might also like