The term "transaction" refers to a unit of labour. Whenever one of these steps fails, the transaction as a whole fails (which is termed as atomicity). ACID attributes can be used to describe a transaction (Atomicity, Consistency, Isolation and Durability).
Hibernate provides a transactional interface for managing data.
Transaction interface defines the unit of work in the Hibernate framework. It maintains abstraction from the implementation of transactional rules. (JTA,JDBC).
Sessions are related with transactions, which can be created by calling session.beginTransaction().
The Transaction interface has the following methods:
The transaction should be rolled back when an exception occurs in hibernation, so that resources can be freed. Let's have a look at hibernate's transaction management sample.
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();
tx.commit();
}catch (Exception ex) {
ex.printStackTrace();
tx.rollback();
}
finally {session.close();}