Easy learning with example program codes

Hibernate transaction management


A transaction is a sequence of operation which works as an atomic unit. A transaction only completes if all the operations completed successfully. A transaction has the Atomicity, Consistency, Isolation and Durability properties (ACID).

Transaction interface:

Transaction interface provides the facility to define the units of work or transactions. A transaction is associated with a session. We have to call beginTransaction()method of Session to start a transaction (Session.beginTransaction()).

Commonly used methods of Transaction interface:

1. begin(): It starts a new transaction.

Syntax: public void begin() throws HibernateException

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

Syntax: public void commit() throws HibernateException

3. rollback(): It roll back the current transaction.

Syntax: public void rollback()throws HibernateException

4. setTimeout(int seconds): It set the transaction timeout for any transaction started by a subsequent call to begin() on this instance.

Syntax: public void setTimeout(int seconds) throws HibernateException

5. isActive(): It checks that is this transaction still active or not.

Syntax: public boolean isActive()throws HibernateException

6. wasRolledBack(): It checks that is this transaction roll backed successfully or not.

Synatx: public boolean wasRolledBack()throws HibernateException

7. wasCommitted():It checks that is this transaction committed successfully or not.

Synatx: public boolean wasCommitted()throws HibernateException

8. registerSynchronization(Synchronization synchronization): It register a user synchronization callback for this transaction.

Syntax: public boolean registerSynchronization(Synchronization synchronization)throws HibernateException


Transaction tx = null;
//Get the session object.
Session session = 
     tx = session.beginTransaction();
     //Perform some operation here
}catch (HibernateException e) {
}finally {

Next Topic: Hibernate example using xml mapping.
Previous Topic: Hibernate mapping file with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at


We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on

Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki