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.

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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books