I wonder if my case is consistency issue...
I have an entity class Player, which has a field lastAttackDate. I set lastAttackDate = sysdate in a transaction, then I commit that transaction, and then I query for players with lastAttackDate < sysdate - 10min (please see the simplified code).
private static final Logger log = Logger.getLogger(MyEndpoint.class.getName());
@ApiMethod(name = "attack")
public MyResult attack(@Named("id") Long id, User user) throws Exception, OAuthRequestException, IOException {
PersistenceManager mgr = null;
Player defender = null;
Transaction tx = null;
try {
mgr = getPersistenceManager();
tx = mgr.currentTransaction();
tx.begin();
long now = new Date().getTime();
defender = getPlayer(mgr, id);//I get player by param ID
defender.setLastAttackDate(now);
tx.commit(); //mgr.close(); mgr = getPersistenceManager();--> I tried that, it did not help
long param = now - 10*60*1000;//sysdate - 10 minutes
Query q = pm.newQuery("select from " + Player.class.getName() +" where lastAttackDate < lastAttackDateParam parameters long lastAttackDateParam");
Set<Player> result = new HashSet<Player>();
result.addAll((List<Player>) q.execute(param));
Iterator<Player> it = results.iterator();
while(it.hasNext())
{
Player p = it.next();
if (p.getLastAttackDate() >= param)
{
log.log(Level.SEVERE, "It really just gave me a result that doesn't meet the criteria");//defender (the recently updated player) falls in this category
it.remove();
}
}
}
finally {
if (tx != null && tx.isActive())
tx.rollback();
if (mgr != null)
mgr.close();
return null;
}
}
What bothers me is that this query still gives me defender as a result. What bothers me even more, is that if I iterate through result and I check if it meets the criteria, I can see that it does not. If it was consistency issue, I'd suspect that getLastAttackDate() should return old, not updated value, but it gives the right one, the most recent one. What do I do wrong? What can I do to make it work?
At the moment, I iterate through the result set and remove the entries that do not meet my search criteria, but it is expensive (reads, cpu time, possibly additional query to try again).