-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Session cache sometimes breaks query contract
PostPosted: Tue Feb 03, 2009 9:36 pm 
Newbie

Joined: Tue Feb 03, 2009 9:23 pm
Posts: 2
Hi All,

I stumped with this issue recently and googled around, but can not find any answer till now. Below is description of the problem:

I have an entity named "operation". It has a timestamp field keeping track of creation timestamp, and a status field with possible values of "successful", "failed", "running".
A simplified version of the OperationDAOHibernateImpl is defined as below:
Code:
public class OperationDaoHibernateImpl implements OperationDao {
  public Operation getLatest() {
    ... // get session logic omitted
    return session.createQuery("from Operation order by timestamp desc").uniqueResult();
  }

  public Operation getLatestSuccessful() {
    ... // get session logic omitted
    return session.createQuery("from Operation where status='successful' order by timestamp desc").uniqueResult();
  }
}


My web application uses OSIV pattern and the request cycle involves below steps
1. load the latest operation by calling operationDao.getLatest() and do something with it.
2. load the latest successful operation by calling operationDao.getLatestSuccessful() and do something with it.

As I am using OSIV pattern, these two steps will be executed in the same hibernate session. Now assume status of the latest operation is changed from "running" to "successful" at the time between step 1 and step 2 by another transaction. The returned object of step 2 will have status of "running" althought where clause of the query states that it wants to retrieve the successful operation due to the fact that hibernate query tries to use cached object in the session which in this case is loaded by step 1.

Using cached object with obsolete state is fine since it does not harm the application generally. However in this case, it breaks the where clause contract and thus breaks "getLatestSuccessful" contract, and could cause big problems for API users as they natually think that the query result should obey to where clause of the query.

What do you think about this issue? Clearing session in getLatestSuccessful method is not preferable as it prevents lazy loading of other objects which breaks my intension of using OSIV pattern. Does the query has an option of bypassing the entity in the first level cache and get object directly from database? (query does have a option of CacheMode.REFRESH, but it is for second-level cache misably).

Regards
Robin


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2009 7:17 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
Hi,

I'd recommend that you provide a specific exclusion in the second query so that the operation that you acquired in query 1 is not eligible for consideration.

Alternatively you could add some version details to the Operation (off the top of my head, I'm not sure whether that will change the cache behaviour or throw an exception when you attempt to persist).

--
Stephen Souness


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 08, 2009 8:22 am 
Newbie

Joined: Tue Feb 03, 2009 9:23 pm
Posts: 2
Thanks for the reply. Exclude the objects of query 1 from query 2 may not be pratical as:
1. there may be many quries before query 2 (query 1 is just used to demonstrate the problem).
2. the actual object to be excluded can not be determined until result of query 2 is returned.

Without any viable workarounds, I am currently writing guarding code after query 2 to check if all returned results matches the query criteria (and throw exception if it does not match). This makes my query logic ugly, but at least my API contract is not breaked.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.