Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting Select Statement on Hibernate Transaction

I have been reading for a while regarding Hibernate but I can't seem to understand one concept regarding Transaction.

On some sites 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. Are transactions needed on Select Statements or not?

like image 763
Mark Estrada Avatar asked Jan 03 '11 06:01

Mark Estrada


People also ask

How do you handle transactions in Hibernate?

Transaction Interface in Hibernate In hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA,JDBC). A transaction is associated with Session and instantiated by calling session.

Does Hibernate lock table during transaction?

Hibernate is not going to do anything to explicitly lock tables you read from. The answer really depends on what Database you're using and what your isolation levels are set to. Locking an entire table by reading rows shouldn't happen in any full featured database written in this century.

Does select need transaction?

If you're sure that all that is happening is a SELECT, then it doesn't need to be in a transaction.

How Hibernate handle multiple transactions?

Therefore, you can run multiple transactions on the same Hibernate Session, but there's a catch. Once an exception is thrown you can no longer reuse that Session. My advice is to divide-and-conquer. Just split all items, construct a Command object for each of those and send them to an ExecutorService#invokeAll .


2 Answers

It depends on the use case.

In a typical CRUD style web application a common entity configuration is to use versioning and optimistic locking. (hibernate annotation docs) If the application is using optimistic locking, dirty reads are probably not that important, and there is no need to put the select in a transaction.

When dirty reads are not acceptable, then a transaction for the select is appropriate. Most of the time, with that kind of scenario, the select will be done in conjunction with some data modification that requires full consistency for a point in time.

like image 110
David Holbrook Avatar answered Oct 20 '22 17:10

David Holbrook


session.getTransaction().commit() is used to persist your changes to database, use it if you changed in database like insert or update

like image 33
Nirmal- thInk beYond Avatar answered Oct 20 '22 18:10

Nirmal- thInk beYond