In the below code:
SessionFactory sessionFactory = new Configuration().configure("student.cfg.xml").buildSessionFactory();
Session session = sessionFactory.openSession();
//Transaction tran = session.beginTransaction();
session.save(student);
//tran.commit();
session.flush();
session.close();
Irrespective of whether I comment/uncomment the Transaction, they works fine i.e. without transaction also save operation is performed successfully. But whn I check the documentation then they always use the transaction before insert update delete. Any specific reasons for the same? Am I missing something?
Please let me know about this. I am fairly confused 🙁
Regards,
When you always use auto-commit, it may harm your db. Let’s say you want to commit bunch of work at a time. In this situation, you need to create transaction and commit all of your work after finishing all the job. For example, you need to do lots of database access for an atomic work for you. In that case, you need to do all the work in your transaction and commit all the work.
Of course with your catch blocks.