Transaction Management

Transaction simply represents a unit of work ,In such case, if one step fails, the whole transaction fails.

Hibernate Transaction Management

Transaction simply represents a unit of work ,In such case, if one step fails, the whole transaction fails.

Transaction is a Interface in Hibernate

  • The methods of Transaction interface are as follows:
    * void begin() starts a new transaction.
    * void commit() ends the unit of work unless we are in FlushMode.NEVER.
    * void rollback() forces this transaction to rollback.
    * boolean wasCommited() checks if the transaction is commited successfully.
    * boolean wasRolledBack() checks if the transaction is rolledback successfully.

A transaction is associated with Session and instantiated by calling session.beginTransaction().

Example of Transaction Management in Hibernate

Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
(Bussiness Logic)
tx.commit();
}catch (Exception ex) {
ex.printStackTrace();
tx.rollback();
}
finally {session.close();}

Transaction associated with Dao layer
spring-config.xml