A database transaction is a single unit of work. It is a set of operations that succeed or fail as a unit.
Properties of a Transaction
Every transaction must have the four properties known as ACID.
- Atomicity : All operations succeed or fail as a unit.
- Consistency : Any transaction must end with consistent state in the database by either committing the data or rolling back to the previous consistent state.
- Isolation : Transactions are isolated and do not affect each other. Concurrent transactions can run but at the end they appear to have run in sequence.
- Durability : After a successful commit the data must be persisted back to DB and thereafter any changes made will not affect the state of the data at the time of commit.
Isolation levels are set for every transaction. It defines the capacity of a transaction to view data that has been updated by another transaction but still not committed. There are four levels of transaction isolation.
As we move from higher level to lower level the performance becomes better but the risk of inconsistent data grows. The isolation levels are as below:
- Serializable : Highest Level. Exclusive Read-Write data access for the transaction. Slowest and least concurrent.
- RepeatableRead : Only data commited by other transaction is readable and multiple reads will yield the same result given no new data has been commited.
- ReadCommited : Only fully commited data can be read.
- ReadUncommited : Uncommited data can be read.
What are the two types of transaction management paradigms provided by JEE?
- Declarative Transaction Management : Uses configuration files.
- Programmatic Transaction Management : Uses Java code.
What are the two main APIs JEE provides for transaction management?
- JDBC : Java Database Connectivity
- JTA : Java Transaction API
What are the six types of transaction propagation property that can be used from declarative transaction?
- Required : Join existing T or else create a new one.
- RequiresNew : Stop existing T, Create a new T and finish it. Complete old T.
- NotSupported : Always run in non-transactional context.
- Supports : Join existing T, don’t create a new one.
- Mandatory : Must be called only from a transaction otherwise TransactionRequiredException is thrown.
- Never : Must NEVER be called from transaction. If called then exception thrown.