-->
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.  [ 10 posts ] 
Author Message
 Post subject: Concurrency and Version
PostPosted: Wed May 10, 2006 12:23 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
I am using the Version property on my persisted objects. If I open the same record on two machines, make changes on Machine A and Save, and then make different changes on Machine B and Save, ... Machine B's changes are not saved.

This is right. But I don't get any errors or other indication that the changes were not saved because the data is out of date.

How do I get such a notification / message? I thought NH would throw an exception. Or something....

--Brian


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 11:19 pm 
Newbie

Joined: Sat Oct 15, 2005 3:30 am
Posts: 17
When you say ManchineA and MachineB - are you refering to 2 browsers hitting a web-app, or windows apps running on different machines?

If it is a web-app, you should be getting a StaleObjectException...

I'm not sure how it works with using NHibernate on a windows app - if each app running on a different machine is using NHibernate internally, then there would be no way for each instance to know the other instance has made changes?

Cheers,

Adam.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 11:49 am 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
adam11235 wrote:
When you say ManchineA and MachineB - are you refering to 2 browsers hitting a web-app, or windows apps running on different machines?


Windows apps running on different machines.

adam11235 wrote:
if each app running on a different machine is using NHibernate internally, then there would be no way for each instance to know the other instance has made changes?


Yes, each windows app uses NH internally - no app server or anything like that.

It seems that NH knows the data is old (stale), as the changes from MachineB did not get persisted to the database.

Prior to using the Version property, it was "Last save wins", but now it is working as I would expect, "first save wins".

I guess what I need to do is look at NH's internals a bit more and figure out how it is using the Version property.


Top
 Profile  
 
 Post subject: NH Update SQL
PostPosted: Fri May 12, 2006 11:29 am 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
I took a look at NH's logs and here is what is happening.

When MachineB tries to save, the SQL is this:

Code:
Update mytable set ........ where mykey = :p8 and nh_version = :p9.


So this is why MachineB does not overwrite MachineA's changes. Because when MachineA saved, it changed the nh_version column and so when MachineB's update statement runs, the where clause will not find any rows to update.

That makes sense. But how do I find out or make a notification on the client app?? I have not worked with interceptors before, but is this what I need to use???

--Brian


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 12:05 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
adam11235 wrote:
If it is a web-app, you should be getting a StaleObjectException...


Actually, I should be getting StaleObjectException for a windows app, too.

There is a call to

Code:
AbstractEntityPersister.Check()


This method throws a StaleObjectStateException if rows < 1. In my case, 0 rows were updated because of the nh_version property, and therfore I expect to see this exception.

I have not actually stepped through the nHibernate code, but it seems like something is wrong here.

If anyone knows why this is, I'd like to hear it. Otherwise I guess I need to be able to step through from my code's save call into the NH code to see why this exception is not being thrown.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 2:19 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
pelton wrote:
Actually, I should be getting StaleObjectException for a windows app, too.


Okay, I seem to be getting this exception, but it is not being caught by any of my catch statements.

The call to NH's SaveOrUpdate looks like this:

Code:
public static void Save(object objectToSave)
{
  ITransaction transaction = Session.BeginTransaction();
  try {

  Session.SaveOrUpdate(objectToSave);
  transaction.Commit();
  }
  catch (NHibernate.StaleObjectStateException staleEx)
  { loging & other stuff here }
  catch (NHibernate.HibernateException hEx)
  { loging & other stuff here }
}


Neither of those log statements are called. Ever.

The code that is actually logging the exception is this. I added an exception handler to my forms application:

Code:
System.Windows.Forms.Application.ThreadException += OnThreadException;

...

public static void OnThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
  log.Fatal("Unhandled Exception Occured", e.Exception);
}


Why would it bypass all of my exception handlers? That doesn't make any sense.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 2:34 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
Please Ignore The Rantings of A Mad Man!!!!

Somehow this is working, though I haven't really changed anything. At least, I'm able to show a Winform Message Box in my StaleException handler.

So at least it is being called, so logging must be some other issue. Who knows, who cares, its Friday, and it compiles!

--Brian


Top
 Profile  
 
 Post subject: About version property
PostPosted: Wed May 17, 2006 1:51 am 
Beginner
Beginner

Joined: Tue Feb 28, 2006 3:16 am
Posts: 28
For using the version property do i need to have the version related column in my database.


Top
 Profile  
 
 Post subject: One more thing to ask related to concurrency
PostPosted: Wed May 17, 2006 1:53 am 
Beginner
Beginner

Joined: Tue Feb 28, 2006 3:16 am
Posts: 28
In ur case it was one object ur saving whose state is changed by someone else, but like in mine case i have object tree which contains various objects and i m saving only root object, how can i handle the concurrency there.


Top
 Profile  
 
 Post subject: Re: About version property
PostPosted: Wed May 17, 2006 1:49 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
pcmedsinge wrote:
For using the version property do i need to have the version related column in my database.


Yes, you must store the version in the database. NH will maintain the value, but the column must exist.

In your mapping file, you must specify the column. This is how I defined mine:

Code:
<version name="NhVersion" column="nh_version" type="long" />


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.