Transactions in Databases
Transactions in Databases
Databases
What is a Transaction
A database transaction is a logical unit of database
operations which are executed as a whole to process
user requests for retrieving data or updating the
database.
Properties of a Transaction
A transaction should have four properties:
i. Atomic.
ii. Consistent.
iii. Isolated.
iv. Durable.
These properties of database transactions are often referred to by the acronym
ACID.
Atomicity
Atomicity means that transactions execute as a whole. This means that a
transaction must be fully complete, saved (committed) or completely undone
(rolled back).
To guarantee atomicity, all operations of the transactions should be reflected
properly in the database or none should.
Atomicity example:
To Transfer funds between bank accounts
Either withdraw and deposit both execute successfully or none of them
In case of failure DB stays unchanged
Consistency
The transaction must be fully compliant with the state of the database as it was
prior to the transaction. This means that the transaction cannot break the
databases constraints such as Primary, foreign, alternate keys
Only valid data will be written to the database.
Example
If a database tables Phone Number column can only contain numerals, then consistency
dictates that any transaction attempting to enter an alphabetical letter may not commit.
Isolation
Transaction data must not be available to other transactions until the original
transaction is committed or rolled back.
This means that when multiple transactions are executing concurrently, then each
transaction is unaware of other transactions executing concurrently in the system i.e.
the execution of one transaction must not interfere with another.
Transactions dont see other transactions uncommitted changes. The isolation level
defines how deep transactions isolate from one another
Read committed, read uncommitted, repeatable read, serializable, etc.
Isolation example:
o Manager can see the transferred funds on one account or the other, but never on
both
Durability
Transaction data changes must be available, even in the event of database
failure. This means that the transaction changes must be persisted or made
permanent.
Transaction durability is ensured through the use of database backups and
transaction logs.
Durability example:
o If after transfer funds and commit changes are made, and the power
supply is lost, the transaction stays persistent
Transaction Manager
The Transaction Manager handles Isolation property of transaction.
Recovery manager
The Recovery Manager handles Atomicity, and Durability.
NOTE:
No dedeicated components of DBMS ensure consistency property of transaction. It is the
responsibility of the programmer to ensure consistency through program code
Transaction States
The following are the different states in transaction processing in a Database
System.
i. Active
ii. Partially committed
iii. Failed
iv. Aborted
v. Committed
Transaction States
1.
2.
3.
4.
5.
6.
Active State
This is the initial state. The transaction stay in this state while it is executing.
Partially Committed.
This is the state after the final statement of the transaction is executed.
Failed State
After the discovery that normal execution can no longer proceed.
Aborted
The state after the transaction has been rolled back and the database has been restored to its state
prior to the start of the transaction.
Committed
The state after successful completion of the transaction.
Terminated.
The transaction has been released for other transactions to transact
Transaction States