Module 7 - Transaction Management (1)
Module 7 - Transaction Management (1)
Transaction
Transaction is the process of performing multiple database operations as one Atomic
unit with all or Nothing Criteria i.e.
o When all the database operations in the unit are successful then Transaction
is successful and should be commited.
o When any one database operation in the unit is failed then Transaction is
failed and should be rolled back.
When you implement transactions properly in your application, It gaurantees ACID
Properties.
A - Atomacity
C - Consistency
I - Isolation
D – Durability
1) Atomacity
Atomacity = Definition of Transaction
Consider the salary transfer
1000 deposits + 1 withdraw => Totally Salary transfer contains 1001 DB Operations.
When all these 1001 database operations are successful then Transaction is
successful and all 1001 DB Operations have to be commited.
When any one DB operation is failed then Transaction is failed and all DB Operations
have to be rolled back.
2) Consistency
When you are running the transaction, you may enforce many business rules as per
the application requirement.
Your application should be consistent when any business rule is failed otherwise you
may get some inconsistent results.
Consider the withdraw operation:
Some business rules are
1) Minimum balance is 5K.
2) if (ATM){
a) Only 5 withdrawals per day.
b) Limit: 50K per day.
}
3) if (Branch){
a) Only 10 withdrawals per day.
b) Limit: 5L per day.
}
When you are implementing the Transaction, You can implement code for Business
rules inside transaction. When any Business rule is failed, then Transaction will be
forced to rollback.
Case B:
Multiple transactions running concurrently are using single Account row of accounts
table.
Customer 1 having Account Number 99 and Balance 15K
At a time, following operations are happening.
1) Transaction 1-trsnafer (withdrawal). - Uses 99
2) Transaction 2-bank teller (withdrawal or deposit). - Uses 99
3) Transaction 3-loan EMI (withdrawal). - Uses 99
In this case,
o 3 Transactions are running concurrently and using single column or row or
table which may cause problems.
The problems coming when multiple transactions running concurrently are called as
Transactional Concurrency Problems.
To avoid these Transactional Concurrency Problems, You have to apply one of the
following required Transactional Isolation Levels.
1) READ_UNCOMMITTED 1
2) READ_COMMITTED 2
3) REPEATABLE_READ 4
4) SERIALIZABLE 8
When TRANSACTION reads the Dirty Values (i.e. modified but not commited) then
you may get some inconsistent results.
To avoid Dirty Reads, you have to lock the Column (Cell).
To Lock the Column, you have to apply Isolation Level called READ_COMMITTED.
When a TRANSACTION is reading the same row repeatedly, you may get differenet
set of values in different reads.This kind of problem is called Repeatable Read
Problem.
To avoid Repeatable Read Problem, you have to lock the Row.
To Lock the Row, you have to apply Isolation Level called REPEATABLE_READ.
When a TRANSACTION is reading the set of rows repeatedly, you may get differenet
set of rows in different reads.This kind of problem is called Phantom Read Problem.
To avoid Phantom Reads, you have to lock the Entire Table.
To Lock the Table, you have to apply Isolation Level called SERIALIZABLE.
1) Local Transactions
When a Single Database is participating in the transactional operations (i.e. DB
operations) then it is called as Local Transactions.
Ex:
Transfer the funds from one account to another account where two accounts are in
same bank or same database.
2) Distributed Transactions
When a two or more databases are participating in the transactional operations then
it is called as Distributed Transactions.
Ex:
Transfer the funds from one account to another account where two accounts are in
different banks or different databases.
Types of Transactions
1) Flat Transactions
2) Nested Transactions
1) Flat Transactions
Ex:
Begin Tx1
OP1
OP2
OP3
End Tx1
Note: Multiple Flat transactions running will not disturb other concurrently running
transactions.
2) Nested Transactions
Ex:
Begin Tx1
1) OP1
2) OP2
3) Begin Tx2
OP3
OP4
End Tx2
4) OP5
5) Begin Tx3
OP6
OP7
End Tx3
6) OP8
End Tx1.
Local Transaction YES YES (JDBC) YES (JDBC) YES (JDBC) YES (JDBC)
Distributed Transaction NO YES (CME) YES (CME) YES (CME) YES (CME)
YES
Flat Transaction YES YES YES YES
YES
Nested Transaction NO NO NO NO
1) JDBC Transactions
By default, Hibernate uses JDBCTransactionFactory which provides
JDBCTransaction.
i.e. Default Transaction management used by hibernate system is JDBCTransactions.
To use JDBCTransactions , Connection Pooing must be
DriverManagerConnectionProvider or C3P0ConnectionProvider
3) CMT Transactions:
To Use CMT Transactions, you must check the following.
o Your Hibernate Application must run in CME (Container Managed
Environment).
o Connections must be Datasource Connections
To Use CMT Transactions, You must specify the following props in hibernate.cfg.xml.