I am aware that I am posting a redundant question. I've gone through various posts in SO and other blogs too, but I've required bit more clarity on couple more items & so posting here.
I use Spring + Hibernate. I am inserting some 'n' records.
MySpringController.java
@Transactional
@RequestMapping(...)
public String saveRecords(@ModelAttribute("orderObj") Order order){
for(Item item : order.getItems()){
itemDAO.save(item);
}
return "saveSuccess";
}
MyHibernateDAO class
public void save(Item item){
session = sf.openSession();
Transaction tx = session.beginTransaction();
session.persist(item);
tx.commit();
session.close();
}
Questions:
There are Transaction enabled in both Spring and Hibernate. Is this good practice to do so? Or transaction in either component is sufficient?
During such bulk transaction, which is advisable to use session.openSession() or session.getCurrentSession()? Is it good practice to open & close transaction every time during a bulk submit?
Now if rollback happens, under which scope it would be? under Hibernate's or Spring's transaction?
Though usage of transaction management varies from application to application, I can point out some important things as per my experience.
You do not need to use both Hibernate and Spring transaction.
Above points are based on my experience and may vary depending on the application requirement.