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: Version checking
PostPosted: Mon Aug 27, 2007 8:10 am 
Newbie

Joined: Fri Aug 10, 2007 5:42 am
Posts: 3
Hi,

thank you for reading (and helping :-)) this!

i activated the version-checking in every table.
When two application-instances use the same dataset at the same time it correctly throws a StaleObjectStateException to the last user who saves changes to that dataset.

The question is: How can i react to this exception?
I thought about throwing away the changes the user did with a session.Refresh(...) that object.
After the Refresh the object has the actual values of the database and also the newest version number.
BUT: When I Flush the Session again, I also get an StaleObjectStateException again?!? WHY?

I use FlushMode.NEVER.

Please look at the code an give me a hint.

[Test]
public void VersionIstNachRefreshAktuell() {
//Installs some test data
int id = BeispielmandantenEinspielen();

//

ISession session1 = pHelper.OpenSession();
Mandant manSession1 = session1.Get<Mandant>(id);

ISession session2 = pHelper.OpenSession();
Mandant manSession2 = session2.Get<Mandant>(id);

Assert.AreEqual(manSession1.Version, manSession2.Version);

//change a value
manSession1.Praefix = manSession1.Praefix + "Geändert";
Assert.AreEqual(manSession1.Version, manSession2.Version);

session1.Flush();
Assert.AreNotEqual(manSession1.Version, manSession2.Version);

//change same dataset in different session
manSession2.Praefix = "WasAnderes";

//session2 can´t save (this exception is correct)
bool exceptionAutgetreten = false;
try {
session2.Flush();
} catch(StaleObjectStateException) {
exceptionAutgetreten = true;
}
if (!exceptionAutgetreten) {
Assert.Fail("StaleObjectStateException did not occure.");
}

session2.Refresh(manSession2);
Assert.AreEqual(manSession1.Version, manSession2.Version);
Assert.AreNotEqual("WasAnderes", manSession2.Praefix);
Assert.AreEqual("objectGeändert", manSession2.Praefix);

//session2 user tries again changing the dataset on the actual data
manSession2.Praefix = "WiederWasAnderes";
session2.Flush();
//again a StaleObjectStateException --- WHY??????
}


Hibernate version:
1.2.0 GA

Name and version of the database you are using:
MSSqlserver 2005

Thanks for your help.
hiaso


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 27, 2007 10:12 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hi,

You shouldn't use a ISession after it has thrown an exception. You have no guarantee that it's in a consistent state.

Your best option is to open a new session, and use that one to reload your object.

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 7:14 am 
Newbie

Joined: Fri Aug 10, 2007 5:42 am
Posts: 3
Hi,

well thanks for your answer.
But as you can imagine it´s not what i wanted to hear :-(

An exception is not an always an error. So I think it´s bad if I am forced to throw the session away every time I get an exception.
Especially in the case of the StaleObjectStateException:
I say thanks to hibernate for the information that another user has edited the dataset but I would like to react to this case in the same session.

I´m a little bit disappointed of hibernate.

Greetings,
Hiaso


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.