Saturday, 21 September 2013

About Spring Transactions Part-1

Transaction Management In Spring Part-1

Question:- What is transaction?


Answer: -A database transaction is a sequence of actions that are treated as a single unit of work. So it should be either committed or completely rolled back.

There are four key properties which help to describe a transaction. These key properties are known as ACID
  1. Atomicity: A transaction should be treated as a single unit of operation which means either the entire sequence of operations is successful or unsuccessful.
  2. Consistency: This represents the consistency of the referential integrity of the database, unique primary keys in tables etc.
  3. Isolation: There may be many transactions processing with the same data set at the same time, each transaction should be isolated from others to prevent data corruption.
  4. Durability: Once a transaction has completed, the results of this transaction have to be made permanent and cannot be erased from the database due to system failure.
A real RDBMS database system will guarantee all the four properties for each transaction. The simplistic view of a transaction issued to the database using SQL is as follows:
  • Begin the transaction using begin transaction command.
  • Perform various deleted, update or insert operations using SQL queries.
  • If all the operation are successful then perform commit otherwise rollback all the operations.

Questions: - What is Isolation?


Answer: - It defines the data contract between transactions.  Following are the possible values for isolation types


S.N.
Isolation & Description
1
TransactionDefinition.ISOLATION_DEFAULT
This is the default isolation level.
2
TransactionDefinition.ISOLATION_READ_COMMITTED
Indicates that dirty reads are prevented; non-repeatable reads and phantom reads can occur.
3
TransactionDefinition.ISOLATION_READ_UNCOMMITTED
Indicates that dirty reads, non-repeatable reads and phantom reads can occur.
4
TransactionDefinition.ISOLATION_REPEATABLE_READ
Indicates that dirty reads and non-repeatable reads are prevented; phantom reads can occur.
5
TransactionDefinition.ISOLATION_SERIALIZABLE
Indicates that dirty reads, non-repeatable reads and phantom reads are prevented.

An isolation level defines how much a transaction may be impacted by the activities of other concurrent transactions. In a typical application, multiple transactions run concurrently and that concurrency may lead towards below problems:-

  • Dirty read
  • Nonrepeatable read
  • Phantom read
  1) Dirty read:- Dirty reads occur when one transaction reads data that has been written but not yet committed by another transaction. If the changes are later rolled back then the data read by the first transaction will be invalid.


Note:- Dirty read is prevented by ISOLATION_READ_COMMITTED isolation level.

2) Nonrepeatable read:-  Nonrepeatable read happen when a transaction executes the same query two or more times and each time the data is different. This is usually happening due to another concurrent transaction updating the data between the queries.



Note:- Dirty and Nonrepeatable read is prevented by ISOLATION_REPEATABLE_READ isolation level.

3) Phantom read:- Phantom read is similar to non repeatable reads. These occur when Transaction A reads a range of records. Meanwhile Transaction B inserts a new record in the same range that Transaction A initially fetched and commit that record . Later when the Transaction A reads the same range again and will also get the record that Transaction B just inserted. This is a phantom read.




Note:-  Dirty, Nonrepeatable and Phantom read is prevented by ISOLATION_SERIALIZABLE
isolation level. 



Questions: - What is Propagation?


Answer: - It defines how transactions relate to each other.These are Transaction attributes which defines how the transaction should behave.

Following are the possible values for propagation types:

S.N.
Propagation & Description
1
TransactionDefinition.PROPAGATION_MANDATORY
Support a current transaction; throw an exception if no current transaction exists.
2
TransactionDefinition.PROPAGATION_NESTED
Execute within a nested transaction if a current transaction exists.
3
TransactionDefinition.PROPAGATION_NEVER
Do not support a current transaction; throw an exception if a current transaction exists.
4
TransactionDefinition.PROPAGATION_NOT_SUPPORTED
Do not support a current transaction; rather always execute non-transactionally.
5
TransactionDefinition.PROPAGATION_REQUIRED
Support a current transaction; create a new one if none exists.
6
TransactionDefinition.PROPAGATION_REQUIRES_NEW
Create a new transaction, suspending the current transaction if one exists.
7
TransactionDefinition.PROPAGATION_SUPPORTS
Support a current transaction; execute non-transactionally if none exists.
8
TransactionDefinition.TIMEOUT_DEFAULT
Use the default timeout of the underlying transaction system, or none if timeouts are not supported.

No comments:

Post a Comment