Hibernate Transaction Management

Transaction is nothing but a unit of work. It is described by four properties: ACID

Atomicity

Consistency

Isolation

Durability

A database transaction has to be as short as possible as it is associated with one Session.

It is instantiated by calling session.beginTransaction().

Sample program for transaction management:

Session session = null;
   Transaction tx = null;
 
    	try{
   session = sessionFactory.openSession(); 
   tx = session.beginTransaction();
   tx.setTimeout(3); //transaction time out of 3 sec
   //doSomething(session);
   tx.commit();
  
  }catch(Exception e){
   tx.rollback(); // whenever exception occur we should rollback 
		//the transaction to maintain atomicity
  }  
finally {
	session.close();
}