Reputation:
What is the difference between transaction management in spring, and that in
hibernate?? I referred many websites, but am still not able to understand
Upvotes: 3
Views: 921
Reputation: 401
One can achieve transaction management using Spring as well as Hibernate but using spring removes lot of repetitive code by handling the rollback/commit internally.
In hibernate
try{
sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.getCurrentSession();
trans= session.beginTransaction();
session.save(txn);
trans.commit();
}catch(Exception e)
{
trans.rollback();
}
In Spring you can just use declarative((@Transactional annotations on you service methods) approach for handling save and commit part and no need to write statements like transaction.commit(); transaction.rollback();
@Transactional
public void addCustomer(Customer c) {
this.customerDAO.addCustomer(c);
}
Which may require lot of conditions checking in case of complex mappings and to save/rollback the transactions in dependent tables. Spring removes all the manual handling of code and by using proxy, it saves the transactions only if all the transactions are executed successfully else rollback whole transactions thus achieving ACID property of transactions.
Moreover hibernate does its job of executing the queries against the database and Spring transaction management takes care of the atomic nature of the transaction thus you can concentrate more on the logic rather than the handling transaction management yourself.
Upvotes: 1