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;

    	try{
    		session = HibernateUtil.getSessionFactory().openSession();
    		tx = session.beginTransaction();
    		tx.setTimeout(5);

    		//doSomething(session);

    		tx.commit();

    	}catch(RuntimeException e){
    		try{
    			tx.rollback();
    		}catch(RuntimeException rbe){
    			log.error("Couldn’t roll back transaction", rbe);
    		}
    		throw e;
    	}finally{
    		if(session!=null){
    			session.close();
    		}
    	}

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.

Do you want to know how to develop your skillset to 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!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
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:
Close