Transaction
Transaction
Lecture 16
Begin
Insert into A (Col1, Col2, Col3)
Select ColX, ColY, ColZ from B
Delete From B
End
Begin
Delete From B
Insert into A (Col1, Col2, Col3)
Select ColX, ColY, ColZ from B
End
Let Have look on this
Suppose that information of stock is stores in database. The database
stores two things i.e. sold list and quantity.
When an item is sold to a customer, it is added to the sold list. This is
one operation on the database.
However, the quantity of the sold item must also be subtracted from the
items currently in stocks.
If both steps are not completed together, the database will be in an
inconsistent state.
The quantity of items in stock will be wrong if items are sold but not
subtracted from items in stock.
The quantity will be wrong if the items are subtracted from items in
stock but not sold.
Both of these operations together make up a single transaction and both
must complete successfully or both must fail.
Sale Table (SaleID, ProductID, Quantity, ClientID)
Stock Table (StockID,ProductID,Quantity)
Begin
Insert into Sale (SaleID, ProductID, Quantity, ClientID) values (100, 3, 7, 1)
Update Stock Set Quantity = Quantity –7 Where ProductID=3
End
Commit and Rollback
A transaction is committed if it completes successfully and
changes the data. If it fails and leaves the data unchanged, it is
said that the transaction has been rolled back.
It means that the rollback is use to undo the work done in the
current transaction.
Transaction Properties
(ACID Properties)
A transaction must have four Properties called ACID
Properties. These are as follows:
Atomicity
Consistency
Isolation
Durability
Atomicity
A transaction must be an atomic unit of work. A transaction
must completely succeed or completely fail. If any statement
in transaction fails, the entire transaction fails completely.
When a transaction is completed successfully it is said to be
committed. In case of failure of a statement, all previous
successful statements in transaction are rollback or reverted
to the previous state of consistency. An incomplete
transaction doesn’t take place.
Consistency
A transaction must leave the data in a consistent state after
completion. For example, in a bank database, money should
never be “created” or “deleted” without an appropriate
deposit or withdrawal.
Isolation
All transactions that modify the data are isolated from each
other. They do not access the same data at the same time.
Transactions must have no dependence or effect on other
transactions. (A transaction is an independent unit of
logically related work)
Durability
The durability means that the modifications made by a
transaction are permanent and persistent. If the system is
crashed or rebooted, data should be guaranteed to be
completed when the computer restarts
Concurrency
Concurrency is a situation in which two are more users
access the same place of data at the same time. In a multi-
user environment, concurrency occurs very commonly. In
some situations, the concurrent access may arise to some
serious problems.
Concurrency Problems
Different problems that may occur due to concurrency are as
follows:
Update Problem
Uncommitted Dependency/Problem
Inconsistent Analysis Problem
Update Problem
The problem arises when two or more transactions update
the same data concurrently.
It occurs when two or more transactions select the same row
and then update the row based on the value originally
selected.
Each transaction is unaware of other transactions.