Transaction in hibernate
What is Transacton in general?
A Transaction represents a unit of work performed within the database management system against a database.
We generally group the related work within one transaction so that if part of the work is failed then entire transaction should be failed.
Transaction has 4 main properties (ACID)
ACID stands for Atomicity,Consistency,Isolation and Durability
Atomicity
If one part of the transaction fails, then Atomicity ensures that entire transaction fails so that database state is left unchanged.
So Atomicity does either entire operation success or no operation success.
Consistency
This will ensures that transaction will bring the database from one valid state to another valid state.
Any data written to the database must be valid according to all defined rules, including constraints,cascades, triggers etc.
So transaction either creates a new and valid state of data, or, if any failure occurs, returns all data to its state before the transaction was started.
Isolation
A transaction which is in progress and not yet committed must remain isolated from any other transaction in the system.
Durability
It ensures that once a transaction has been committed, it will remain permanently in the system.
Transaction in Hibernate
A transaction is associated with a Session and is usually instantiated by a call to Session.beginTransaction().
A single session might span multiple transactions However, it is intended that there will be at most one uncommitted Transaction associated with a particular Session at any time.
Methods used in Hiernate transaction
public void begin() : It starts a new transaction
public void commit() : This method will commit the associated transaction
public void rollback() : This method will force the underlying transaction to roll back
It can be used whenever some exception occurred.
public boolean isActive() : This method will return true if the local transaction is still active,false otherwise
public void setTimeout(int seconds) : This method will set the transaction timeout for any transaction started by a subsequent call to begin() on this instance
public boolean wasCommitted() : This method will check if the transaction is commited successfully or not.
public boolean wasRolledBack() : This method will check if the transaction is rolledback successfully or not.
Transaction in hibernate example
It is advisable to handle the exception and rollback the transaction in catch block as shown below
- Session session = null;
- Transaction tx = null;
- try {
- session = HibernateUtil.getSessionFactory().openSession();
- tx = session.beginTransaction();
- tx.setTimeout(5);
- //some code to execute within transaction
- tx.commit();
- }
- catch (Exception ex) {
- try{
- tx.rollback();
- }catch(Exception exception){
- log.error("Couldn’t roll back the transaction", exception);
- }
- throw ex;
- }
- finally{
- if(session!=null)
- {
- session.close();
- }
- }
Session session = null; Transaction tx = null; try { session = HibernateUtil.getSessionFactory().openSession(); tx = session.beginTransaction(); tx.setTimeout(5); //some code to execute within transaction tx.commit(); } catch (Exception ex) { try{ tx.rollback(); }catch(Exception exception){ log.error("Couldn’t roll back the transaction", exception); } throw ex; } finally{ if(session!=null) { session.close(); } }
Can you add Hibernate n+1 problem and its solution programmatically to your hibernate programs.All you tutorial is too good as it is a practical approach to each topic.Thanks a lot for you explanations.