Why do I need Transaction in Hibernate for read only operation?
Does the following transaction put a lock on db?
Example code to fetch from db:
Transcation tx = HibernateUtil.getCurrentSession().beginTranscation(); //why begin transcation?
//readonly operation here
tx.commit() // why tx.commit? i dont want to write anything
You might actually have reasons to mark transactions as read-only.
autocommit=trueif different option wasn't set explicitly.
@Transactional(readonly=true), Spring will set the JDBC transaction into a read-only mode, thus you'll dictate whether it's actually possible to write into DB in scope of this transaction. If you architecture is pretty cumbersome and some team members might not follow reasonable path, this flag would point you to the problematic place.
To sum up - you can go both ways, but you need to understand consequences.