为什么do I need Transaction in Hibernate for read-only operations?
Does the following transaction put a lock in the DB?
Example code to fetch from DB:
Transaction tx = HibernateUtil.getCurrentSession().beginTransaction(); // why begin transaction? tx.commit() // why tx.commit? I don't want to write anything
Can I use session.close() instead of tx.commit()?
It doesn't matter whether you only read or not - the database must still keep track of your result set, because other database clients may want to write data that would change your result set.
I have seen faulty programs to kill huge database systems, because they just read data, but never commit, forcing the transaction log to grow, because the DB can't release the transaction data before a COMMIT or ROLLBACK, even if the client did nothing for hours.
Hope this is helpful!!