Example & Tutorial understanding programming in easy ways.

Explain Hibernate Transaction API?

New Page 1

Hibernate Transaction API: In Hibernate Transaction API, A transaction simply represents a unit of work. In such case, if we get one step fails, then the whole transaction fails(which is termed as atomicity). A transaction can be described by ACID properties (Atomicity, Consistency, Isolation and Durability).

Example: If we take a work in the initial state then we perform the any transaction if commit or (Action depended like transaction ) then Transaction succeeded other wise if we do not perform the work as like the behaviour of the transaction the we roll back that time our transaction going to be failed.

Transaction Interface in Hibernate:

In hibernate framework, we have transaction interface that defines the unit of work. It maintains  abstraction from the transaction implementation (JTA, JDBC).

When we perfom a task then we se a transaction then we found that A transaction is associated with Session and instantiated by session.beginTransaction().

The method of Transaction interface are as follows:

1. void begin() -> It start to a new connection.

2. void commit() -> It end the unit of work unless we are FlushMode.Never.

3. void rollback() -> It forces this transaction to rollback.

4. void setTimeout(int seconds) -> It sets a transaction timeout for any transaction started by a subsequent call to begin on this interface.

5. boolean isAlive() -> check if the transaction is still alive.

6. void registerSynchronization(Synchronization s) -> registers a user synchronization callback for this transaction.

7. boolean wasCommited() -> checks if the transaction is commited successfully.

8. boolean wasRollBack() -> checks if the transaction is rolleback successfully.

Another Example for the Transaction Management in Hibernate: In the example of the hibernate it is better to rollback the transaction if any exception occurs, so that resources can be free. 

Now see the example:

Session session = null;

Transaction tx = null;

try {

session = sessionFactory.openSession();

tx = session.beginTransaction();

//some action


}catch (Exception ex) {




finally {session.close();}

Read More →
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!