Hibernate Transaction Management

By | | Updated : 2021-03-10 | Viewed : 573 times

Hibernate Transaction Management

The current tutorial describes how transactions should be handled in the database and how Hibernate will take care of their transactions by using transaction management. So we are gonna to learn the transaction and its working mechanism in detail with clear examples.

Before thinking about transaction management we should have to think about ACID properties where properties will play a main and bigger role in transaction management.

ACID Properties in Database

While handling operations in the Database, the ACID properties play a key role. ACID ensures the Database\'s state properly by handling the transaction\'s state. In maintaining the application with the database, we need to recognize every transaction state whether succeeded or not.

1. Atomicity:

This property treats every transaction as a single unit in maintaining the database state. If some of the transaction statements are failed in the middle of the execution then the remaining transaction will be reverted.

2. Consistency:

This property addresses the consistency of the Database\'s state. Database State should remain the same before and after the execution of any transaction.

3. Isolation:

In the concurrent execution of multiple transactions, the Isolation property keeps the database\'s state consistent. Unfinished transactions will not affect others in this concurrent execution. In other words, no transaction will not affect any other transaction.

4. Durability

Durability means that transaction once complete it is not possible to revert an exception in case of system failures.

Transaction in Hibernate

As per the definition of Hibernate, a transaction is a single unit of work which means a set of SQL command to be executed. So transactions should be handle properly otherwise it may impact the database\'s state. Please find the below-given diagram represent the transaction in hibernate.

Usually, Transaction contains multiple SQL statements to be executed in the Database. Once the transaction is completed it should be committed then that data will be stored in the database. In case of transaction failed data will not be stored in the database.

Transaction Interface in Hibernate

Transaction Interface lets application allows for producing the transactions as well sessions which will be used for performing the DB operations. It worked under defined transaction management. Transaction Management should follow the guidelines of org. hibernate.Transaction.

We will see the example of code for Hibernate Transaction Management.

Hibernate Transaction Management Example
Session session = null;
Transaction transaction = null;

try {
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
    //DB Operations to be perfomed

    transaction.commit();

} catch (Exception ex) {
    ex.printStackTrace();
    transaction.rollback();
} finally {
    session.close();
}

Notice above given code snippet for Hibernate Transaction Management. The Hibernate session will be created by the SessionFactory. Each SessionFactory will be aligned with a single database and one is sufficient. Here SessionFactory will be used for creating the Session which is nothing but a bounded time in between the beginning and end of the transaction.

Steps for handling Transaction Management

  • Create the Session using with SessionFactory using with sessionFactory.openSession().

  • Generate the Transaction to perform Database operations for business using Session as session.beginTransaction();

  • After completion of the Database operation, it is required to reflect all the changes in DB. So for that, it is required to commit the transaction with the transaction.commit() method.

  • All operation changes have reflected in the Database. It is time to close the Session and Transaction in case there is no execution. In Finally block of java, the session should be closed with Session.close();

  • If an exception is raised then the transaction should be closed to avoid Database inconsistency data. For that we need to roll back the transaction with tx.rollback();

Leave A Reply