-->
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: StaleObjectStateException does not rollback child object
PostPosted: Tue Jan 31, 2012 10:14 pm 
Newbie

Joined: Tue Jan 31, 2012 10:04 pm
Posts: 3
Hi,
Iam relatively new to hibernate. Encountering a weird scenario where multiple threads are working on the same parent object. The parent has a <one-to-many> child Map (inverse="true"). Basically each of the thread adds different values of the child attribute with the same key. For eg.
ParentA initially has no child attributes.
Thread1 -> ParentA -> (key1, value1)
Thread2 -> ParentA -> (key1, value2)

The logic determines if there are any values for the key, and if it's null creates a new child object with the key/value and sets the parent instance. If value is a non-null it updates the value.
So in the above scenario there are two child objects created with the same key. When Thread1 updates the parent, the child objects is also persisted. When Thread2 updates there is a StaleObject thrown, there is retry logic that refreshes the session, and reloads the data. So there should be ONLY one child object key1 with value2. But there are two records key1, value1 and key2, value2 persisted in the database.
Any suggestions?


Top
 Profile  
 
 Post subject: Re: StaleObjectStateException does not rollback child object
PostPosted: Fri Feb 03, 2012 4:19 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
there is retry logic that refreshes the session, and reloads the data.


Can you please post the code of this your retry logic.
Do you also use Cascade on persist option?


Top
 Profile  
 
 Post subject: Re: StaleObjectStateException does not rollback child object
PostPosted: Fri Feb 03, 2012 2:24 pm 
Newbie

Joined: Tue Jan 31, 2012 10:04 pm
Posts: 3
pb00067 wrote:
Quote:
there is retry logic that refreshes the session, and reloads the data.


Can you please post the code of this your retry logic.
Do you also use Cascade on persist option?


Apologize for not posting it earlier. Didn't want to spam with code.
The gist of the retry logic is as follows-
boolean success = false;
while (!success) {
try {
processOlapQuery(idIterator, outputFileHandler, helper, operationHandler);
success = true;
} catch (StaleObjectStateException e) {
//first we'll restart session
DAOSessionHelper.endSession();
DAOSessionHelper.startSession();
//then reset input, output, and helper/handler
idIterator.reset();
outputFileHandler.reset();
helper.reset();
operationHandler.refresh();
//check this AFTER we reset everything, so session is in a usable state
if (staleStateCount > MAX_STALE_STATE_RETRIES) {
logger.error("Un-recoverable error (too many stalestates!): " + e.getMessage(), e);
throw e;
}
staleStateCount++;
}
}

And yes the map property of the child in the parent config file has the cascade as
cascade="all,delete-orphan"


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.