Table of contents
1.
Introduction
1.1.
Transaction interface
2.
Hibernate Transaction Management methods
2.1.
Example
3.
Frequently Asked Questions
3.1.
What is a Session in Hibernate?
3.2.
What is a SessionFactory?   
3.3.
What do you think about the statement - “session being a thread-safe object”?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

Hibernate Transaction Management

Author Prerna Tiwari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will learn about the transaction and hibernate transaction management with examples. A transaction is a unit of work. If one step fails in this case, the entire transaction fails (which is termed atomicity). ACID properties (Atomicity, Consistency, Isolation, and Durability) can be used to describe a transaction.

Hibernate Transaction Management

Hibernate Transaction Management

Transaction interface

The transaction interface allows you to define work units or transactions. A transaction is linked to a session. To begin a transaction, we must use the session.beginTransaction() method (Session.beginTransaction()).

Now let’s see the methods of hibernate Transaction Management.

Hibernate Transaction Management methods

 

  1. begin(): It starts a new transaction.
    Syntax: 
     
public void begin() throws HibernateException

 

2. commit(): It ends the transaction and flushes the associated session.
    Syntax: 
 

public void commit() throws HibernateException

 

3. rollback(): It rolls back the current transaction.
    Syntax:
 

 public void rollback()throws HibernateException

 

4. setTimeout(int seconds): It specifies the transaction timeout for any subsequent calls to begin() on this instance.
    Syntax: 
 

public void setTimeout(int seconds) throws HibernateException

 

5. isActive(): It determines whether or not the transaction is still active.
    Syntax: 
 

public boolean isActive()throws HibernateException

 

6. wasRolledBack(): It determines whether or not the transaction was successfully rolled back.
    Syntax:
 

 public boolean wasRolledBack()throws HibernateException

 

6. wasCommitted(): It determines whether or not the transaction was successfully completed.
    Syntax: 
 

public boolean wasCommitted()throws HibernateException

 

7. registerSynchronization(Synchronization synchronization): For this transaction, it registers a user synchronisation callback.
    Syntax: 
 

public boolean registerSynchronization(Synchronization synchronization)throws HibernateException

Example


We can use the various methods of hibernate transaction management by using the following pieces of code .
 

//Here is one example of uses of  hibernate Transaction Management methods.


Transaction ty = null;


//Get the session object.
Session session = 
        HibernateUtil.getSessionFactory().openSession();
try{
     ty = session.beginTransaction();
     //Perform some operation here
     tx.commit();


}catch (HibernateException e) {
     if(ty!=null){
          ty.rollback();
     }


     e.printStackTrace(); 


}finally {
         session.close(); 
}


In hibernate, it is better to rollback the transaction if any exception occurs, so that resources can be free.

Frequently Asked Questions

What is a Session in Hibernate?

A session is an object that keeps the connection between the Java object application and the database alive. Session also includes methods for storing, retrieving, modifying, and deleting data from a database, such as persist(), load(), get(), update(), delete(), and so on. It also includes factory methods for returning Query, Criteria, and Transaction objects.

What is a SessionFactory?   

SessionFactory returns a Session instance. It is a factory class that returns Session objects based on configuration parameters in order to connect to the database.

As a best practice, the application should only have one instance of SessionFactory. The internal state of a SessionFactory, which includes ORM metadata, is immutable, which means it cannot be changed once the instance is created.

This also allows you to obtain information such as statistics and metadata about a class, query executions, and so on. If enabled, it also stores second-level cache data.

What do you think about the statement - “session being a thread-safe object”?

No, the session is not a thread-safe object, which means that multiple threads can access it at the same time.

Conclusion

In this article, we have discussed the concepts of hibernate Transaction Management. We started by introducing hibernate Transaction Management and transaction interface, and finally, we discussed the methods of hibernate Transaction Management.

We hope that this blog has helped you enhance your knowledge of hibernate Transaction Management. If you want to learn more, check out our article on the phases of the compiler.  

For peeps out there who want to learn more about Data Structures and Algorithms, Java, Python, or any other upskilling, please refer to guided paths on Coding Ninjas Studio. Enroll in our courses, go for mock tests and solve problems available and interview puzzles. Also, you can put your attention towards interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help other ninjas grow.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass