Nikos Maravitsas

About Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.

Hibernate Transaction Handle Example

This tutorial is going to talk about the basics of Hibernate Transactions and Sessions. As we know modern database systems use the notion of Transaction and Session in order to define a group of data accessing operations. When multiple users access the same database data, the system has to ensure that all the operations are going to be executed normally and the database will end up in a consistent state.

1. Unit Of Work – Session

A unit of work is, as we said, a group of database operations. Hibernate uses Session to express the notion of the unit of work. In order to begin a Unit of Work you have to open a Session and to end it you have to close a Session. Session is basically a cache and an interface for all the operation the user wants to do in a database.

2. Transaction

A Transaction is also a group of operations over a database but it defines the very boundaries of these operations. A transactions has a begining, a set of operations that will all be executed correctlly or else none of them will be executed at all, and finally  an end. When a database system has to handle many transactions, it is responsible to optimize the  chronicle of the different operations and ensure its correctness. In Hibernate no operations can happen out of the scope of a transaction. To manage transactions Hibernate also uses JTA (Java Transaction API). When using JTA all database connections are part of the global JTA transaction.

3. Example

This is the basic structure that your Hibernate programs should have, conserning transaction handling:

        Session session = null;
    	Transaction tx = null;

    		session = HibernateUtil.getSessionFactory().openSession();
    		tx = session.beginTransaction();



    	}catch(RuntimeException e){
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		throw e;

Here as you can see whenever a RuntimeException happens we call rollback() API call that forces the rollback of the transaction. This means that every operation of that specific transaction that occured before the Exception, will be canceled and the database will return in its state before these operations took place.

Useful links:

This was an example on Hibernate Transaction Handle.

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: