1

I'm using jboss, and I have a class using Hibernate criteria query to execute Entity read from a db.

I've implemented the query with

// org.hibernate.Criteria crit ..
// crit.add(Restriction...
// crit.setProjections(...
List<MyObject> l = crit.list()

during the application execution, another application change the db content . So If I read again in the same session with another c.list() the data seems not changed, because the hibernate cache returns to me the 'old' data.

With refresh I can do something like (starting from EntityManager em to get Session):

for (MyObject o : l) {
     ((Session)em.getDelegate()).refresh(o); 
}

It works, it refresh the object content, with the updated data coming from the db instead of from the cache. But this is highly inefficient, because it refresh one by one all the object in the list, and they could be a lot.

Another way is close and reopen the session, but I dont' wont do it.

I tried with ((Session)em.getDelegate()).setCacheMode(CacheMode.REFRESH) and with crit.setCacheable(false) but in both cases all the read execution returns to me the 'old' version of the data from the cache.

Do you have any suggstion, to set in the session / criteria to execute the query from the db, instead of from the cache? In my application usage the cache is ok, except in one case as explained above, where the data on the db are changed from outside, from another session of another application.

Thank you

Francesco
  • 315
  • 1
  • 3
  • 13
  • try this http://stackoverflow.com/questions/3827704/how-to-disable-hibernate-caching – Pokuri Oct 25 '13 at 12:34
  • Thanks Pokuri, I tried with cacheMode IGNORE: the first turn I reach the breakpoint, the CacheMode in session was set to NORMAL; setting it to ignore, every next read the session cache mode is still to IGNORE, but the data comes from cache anyway. – Francesco Oct 25 '13 at 13:02
  • You may use transaction isolations to deal with this. Did you try read uncommitted isolation level? – Zeus Oct 25 '13 at 14:28
  • Another approach would be to try stateless session, it does not have cache, so, you ready only from DB – Zeus Oct 25 '13 at 14:29
  • What I can suggest in that case is create another empty class extending your querying class and map it in hbm but this time without cache tag. Use this new class where you need to hit DB all the time to get the records. – Pokuri Oct 26 '13 at 15:07

0 Answers0