2. Contents
ā¢ Introduction to transactions
ā¢ Transaction Control
ā¢ Properties of transactions
ā¢ States of transactions
3. What is a Transaction?
ā¢ A transaction is a basic unit of program
execution which possibly access and update
various data items in a database.
ā¢ Collection of operations that form a single
logical unit of work are called transactions.
4. ā¢ A transaction is delimited by statements of the
form begin transaction and end transaction.
ā¢ The transaction consists of all operations
executed between the begin transaction and end
transaction.
5. Example of Transaction
ā¢ A withdrawal transaction in an ATM could
consist of following steps:
ā« Verify account details
ā« Accept withdrawal request
ā« Check balance
ā« Update balance
ā« Dispense money
6. Transaction control
ā¢ The following commands are used to control
transactions.
ā« COMMIT ā to save the changes.
ā« ROLLBACK ā to roll back the changes.
ā« SAVEPOINT ā creates points within the groups of
transactions in which to ROLLBACK.
ā« SET TRANSACTION ā Places a name on a
transaction.
7. Commit command
ā¢ The commit command is the transactional
command used to save changes invoked by a
transaction to the database.
ā¢ The syntax for the commit command is as
follows:
ā« COMMIT;
ā¢ Example :
ā« sql?>DELETE FROM CUSTOMERS WHERE AGE
= 25;
ā« sql> COMMIT;
8. Rollback command
ā¢ The rollback command is the transactional
command used to undo transactions that have
not already been saved to the database.
ā¢ The syntax for a rollback command is ā
ā« ROLLBACK;
ā¢ Example :
ā« SQL> DELETE FROM CUSTOMERS WHERE
AGE = 25;
ā« SQL> ROLLBACK;
9. Savepoint command
ā¢ A savepoint is a point in a transaction when you
can roll the transaction back to a certain point
without rolling back the entire transaction.
ā¢ The syntax for a savepoint command is:
ā« SAVEPOINT SAVEPOINT_NAME;
10. Set transaction command
ā¢ The set transaction command can be used to
initiate a database transaction.
ā¢ For example we can specify a transaction to be
read only or read write.
ā¢ The syntax for a set transaction command is:
ā« SET TRANSACTION [ READ WRITE | READ
ONLY ];
11. Properties of Transactions
ā¢ In order to maintain consistency in a database,
before and after transaction, certain properties
are followed.
ā¢ These are called ACID properties.
ā« A: Atomicity
ā« C: Consistency
ā« I: Isolation
ā« D: Durability
12. Atomicity
ā¢ It means that the entire transaction takes place
at once or doesnāt happen at all.
ā¢ There is no midway i.e. transactions do not
occur partially.
13. ā¢ It involves following two operations.
ā« Abort: If a transaction aborts, changes made to
database are not visible.
ā« Commit: If a transaction commits, changes made
are visible.
ā¢ Atomicity is also known as the āAll or nothing
ruleā.
14. Consistency
ā¢ This means that integrity constraints must be
maintained so that the database is consistent
before and after the transaction.
ā¢ It refers to correctness of a database.
15. Isolation
ā¢ This property ensures that multiple transactions
can occur concurrently without leading to
inconsistency of database state.
ā¢ Transactions occur independently without
interference.
ā¢ Changes occurring in a particular transaction
will not be visible to any other transaction until
that particular change in that transaction is
written to memory or has been committed.
16. Durability
ā¢ This property ensures that once the transaction
has completed execution, the updates and
modifications to the database are stored in and
written to disk.
ā¢ These updates now become permanent and are
stored in a non-volatile memory.
ā¢ The effects of the transaction, thus, are never
lost.
17. Transaction States
ā¢ While the transaction executes in a database it
could be in one of the following states:
ā« Active : In this state, the transaction is being
executed. This is the initial state of every
transaction.
ā« Partially committed: When a transaction
executes its final operation, it is said to be in a
partially committed state.
18. ā« Failed : A transaction is said to be in a failed state
if any of the checks made by the database recovery
system fails. A failed transaction can no longer
proceed further.
ā« Aborted: If any of the checks fails and the
transaction has reached a failed state, then the
recovery manager rolls back all its write operations
on the database to bring the database back to its
original state where it was prior to the execution of
the transaction. Transactions in this state are
called aborted.
ā« Committed: If a transaction executes all its
operations successfully, it is said to be committed.
All its effects are now permanently established on
the database system
19.
20. Demonstrating transaction states
ā¢ Consider a user making a booking for a flight
over a web.
ā¢ While the user is selecting the time for departure
and the seat available, the transaction is said to
be in active state.
ā¢ When the user is making the payment for the
tickets, the transaction is said to be partially
commited.
21. ā¢ While making the payment if the system crashes
or network connection goes off, the transaction
may be aborted.
ā¢ Otherwise, if successful payment is made the
transaction is said to be committed.