2

I'm using Hibernate transaction for read operation from db as following excample code:

Session session = sessionFactory.openSession();    
Transaction tx= session.beginTransaction();
session.get(Account.class, new Long(id));
tx.commit(); //Actually I use tx.rollback()
session.close();

Due to the second level cache, result is fetched from cache after the first time reading from db. But the database connection is distributed when session.beginTransaction() is invoked, and next commit statement will be executed at database, which degrades the performance dramatically(compared to the case when no transaction is used).

So is there any possible way to know that the result will be read from second level cache so I can avoid doing commit and even use a new database connection?

taoxiaopang
  • 407
  • 1
  • 4
  • 13

1 Answers1

0

Assuming you are using spring transaction support, have your tried using SUPPORTS propagation level?

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113