Putting Select Statement on Hibernate Transaction
- by Mark Estrada
Hi All,
I have been reading the net for a while regarding Hibernate but I can seem to understand one concept regarding Transaction.
On some site that I have visit, Select statements are in transaction mode like this.
public List<Book> readAll() {
Session session = HibernateUtil.getSessionFactory()
.getCurrentSession();
session.beginTransaction();
List<Book> booksList = session.createQuery("from Book").list();
session.getTransaction().commit();
return booksList;
}
While on some site, it does not advocate the use of transaction on Select statements
public List<Book> readAll() {
Session session = HibernateUtil.getSessionFactory()
.getCurrentSession();
List<Book> booksList = session.createQuery("from Book").list();
return booksList;
}
I am thinking which one should I follow. Any thoughts please? Are transactions needed on Select Statements or not? Thanks