-->
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.  [ 5 posts ] 
Author Message
 Post subject: Graceful recovery from StaleObjectStateException
PostPosted: Wed Jan 28, 2009 4:06 pm 
Newbie

Joined: Wed Jan 28, 2009 3:49 pm
Posts: 3
Hi. I'm using Hibernate 3.2.5 with an EHCache.

The code below, on an entity declared with a <version> in it's mapping, fails with StaleObjectStateExceptions that I can't seem to recover from, even though I try many times to reload the current state.

Code:
// update service statistics when a resource is fetched from a resource manager
for(int attempts = 1; attempts <= lockRetryAttempts; attempts++) {
    try {
        session.evict(manager);
        manager = (ResourceManager) session.load(ResourceManager.class,
            manager.getId(), LockMode.READ);
        // sleep for a few seconds here to highlight concurrent updates
        manager.setFetchedBytes(manager.getFetchedBytes() + resource.getSize());
        manager.setFetchedFiles(manager.getFetchedFiles() + 1);
        session.flush();
        break;
    } catch(StaleStateException e) {
        if(attempts == lockRetryAttempts) {
            throw e;
        }
    }
}


So, how can I get Hibernate to do what I'm trying to do? Which is, when catching a StaleObjectStateException, to force it to reload the record and try again.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 6:11 am 
Newbie

Joined: Wed Jan 28, 2009 3:49 pm
Posts: 3
Bump? Any ideas? It can't be that uncommon, to want to catch and recover from what should be an unexceptional and expected event when dealing with optimistic locking?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 6:49 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Try to get a new session, as you should not use the same session after an error occured.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2009 2:42 pm 
Newbie

Joined: Wed Jan 28, 2009 3:49 pm
Posts: 3
Is there really no better way? It just seems a bit wasteful and inefficient to have to rollback and tear down the session, create a new one, start a new transaction, and redo (somehow...) all the updates to that point just to retry an optimistically locked update, when all that's failed is the 'WHERE version = :currentversion' part of an update during the flush returned 0 rows updated.

Why can't I just refresh the state of the stale object and attempt the update again, with the version counter now current? Like I said, detecting a version conflict on a heavily contended table is hardly an exceptional circumstance, so why does Hibernate seem to demand such drastic corrective action as destroying the session and restarting if I catch it and try to handle it?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 6:41 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I guess, there is no "better" way. See http://www.hibernate.org/hib_docs/v3/reference/en-US/html_single/#transactions-demarcation-exceptions.
I think it won't be to inefficient to use a new session. Obtaining a new session is not very expensive and you could merge your "dirty" objects to do the updates.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.