Putting Select Statement on Hibernate Transaction

Posted by Mark Estrada on Stack Overflow See other posts from Stack Overflow or by Mark Estrada
Published on 2011-01-03T06:47:32Z Indexed on 2011/01/03 6:53 UTC
Read the original article Hit count: 282

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate