Database · Java · Programming · transaction

Database Transaction Basics using Java

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
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:

  1. Serializable : Highest Level. Exclusive Read-Write data access for the transaction. Slowest and least concurrent.
  2. RepeatableRead : Only data commited by other transaction is readable and multiple reads will yield the same result given no new data has been commited.
  3. ReadCommited : Only fully commited data can be read.
  4. ReadUncommited : Uncommited data can be read.

What are the two types of transaction management paradigms provided by JEE?

  1. Declarative Transaction Management : Uses configuration files.
  2. Programmatic Transaction Management : Uses Java code.

What are the two main APIs JEE provides for transaction management?

  1. JDBC : Java Database Connectivity
  2. JTA : Java Transaction API

What are the six types of transaction propagation property that can be used from declarative transaction?

  1. Required : Join existing T or else create a new one.
  2. RequiresNew : Stop existing T, Create a new T and finish it. Complete old T.
  3. NotSupported : Always run in non-transactional context.
  4. Supports : Join existing T, don’t create a new one.
  5. Mandatory : Must be called only from a transaction otherwise TransactionRequiredException is thrown.
  6. Never : Must NEVER be called from transaction. If called then exception thrown.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s