-->
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: Real world patterns using optimistic locking and recovery
PostPosted: Fri Mar 12, 2004 10:34 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Hi there, I'm trying to wrap my head around using optimistic locking
properly.

When I say properly, I mean being able to recover from
StaleObjectStateExceptions and do the right thing.

So far, I have something that looks something like this:

Code:
private void updateExistingRef(Ref ref, ConnectionState state)
{
    boolean committed = false;
    for (int i=0; i < 2; i++)
    {
        if (i > 0)
        {
            ref = (Ref) HibernateManager.load(ref.getClass(), ref.getId());
        }
        try
        {
            updateRefProperties(ref);
            state.commitTransaction(true);
            committed = true;
            break;
        }
        catch (StaleObjectStateException e)
        {
            state.rollbackTransaction();
            state.releaseHibernateSession();
        }
    }
    if (!committed)
    {
        // fallback to pessimistic locking
        state.setLockMode(LockMode.UPGRADE);
        ref = (Ref) HibernateManager.load(state, ref.getClass(), ref.getId());
        updateRefProperties(ref);
        state.commitTransaction();               
    }
}


HibernateManager and ConnectionState are pretty straightforward
wrappers for handling exceptions, giving access to the session and transaction, etc.

I'm just wondering what other people are doing in "the real world"
where it's unacceptable to lose data, so error recovery has to be
dealt with appropriately.

Have people come up with decent abstractions for these kind
of "recoverable/retryable" units of work, or is there a better way
of doing this in Hibernate and I've missed it, or what?

In this example, retrying is simple, all the updates are handled by
the
Code:
updateRefProperties
method, but many more
complicated examples where updates aren't isolated in the same
way could quickly get unwieldy.

Any thoughts welcome...

Hunter


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 14, 2004 6:57 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
How autorecovery for optimistic locking is better than no optimistic locking ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 6:12 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 6:52 am
Posts: 46
Is there any kind of automatic recovery? From what I can tell,
you pretty much have to "roll your own" recovery/retry code.

I'm just wondering if anybody has come with nice/interesting/whatever patterns/abstractions/classes for making this kind of recovery easy.

H
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 7:24 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It will be the same as no optimistic locking if last saved wins.
It is possible to merge versions in some cases (version conflicts, but fields do not), but field conflict must be handled any way and only input source knows how to handle it (an analogy can be CVS).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 7:14 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Here is a relevant thread:

Single centralized thread on recovering from stale object exceptions

Let's try to join forces there and break out of the stale dogma of StaleObjectStateExceptions!!!


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.