-->
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: Flush vs Update vs Transaction
PostPosted: Thu Sep 08, 2005 8:11 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
I am coming to NHibernate from another OrMapper so I'm having a little trouble grasping the flush concept.

In the other OrMapper I used, all property edits and updates were performed within a transaction. So when I called commit I knew exactly what data would be updated to the database.

I'm following the repository pattern on this particular project so all references to NHibernate are encapsulated within the repositories. Since NHibernate is hidden from the UI, what is the best way to actuall persist the updates I have made to any object?

I was thinking that i could simply have have an update operation on my repository that would take the modified object as an argument. I would then call flush from within the update operation. This could be misleading though since all objects that had been modified would also be updated since flush had been called.

It doesn't seem like session.Update will work for me because I'm not really disconnecting my object from the session. Wrapping session.Update seems perfect, I'm just not sure why it was designed to only work on detached objects.

I then though that I could wrap the transaction of a session and just require the application to place all object modification within a transaction and call commit to update the object. But I have read that calling commit on a transaction also calls flush.

Is there any way to have the session abstracted from the application layer and still be explicit about updates?

Any help with a pattern here would be great. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 12:39 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
The common way to do a "job" is:

Code:
ISession sess = factory.OpenSession();
ITransaction tx;
try {
   tx = sess.BeginTransaction();
   //do some work
   ...
   tx.Commit();
} catch (Exception e) {
   if (tx != null) tx.Rollback();
   throw;
} finally {
   sess.Close();
}


You shouldn't call Flush() unless you really need to!

Further reading: Defensive session handling (on Hibernate website)

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 12:55 pm 
Senior
Senior

Joined: Thu Jun 02, 2005 5:03 pm
Posts: 135
Location: Paris
Hey jnapier,

I had the same sort of problem. The way I resolved it was to add an interface to my entities (IEntity) which had a property SaveRequested.

Code:
   public interface IEntity
   {
      bool SaveRequested
      {
         get;
         set;
      }
}


When the user calls the Update or Save methods of my repository I set SaveRequested = true, execute the Save()or Update() and then set SaveRequested = false.

I then added an interceptor and imlemented a check in the FindDirty callback to check whether the entity that arrived had the SaveRequested flag true - if it didn't then I return an empty int[] as specified in the Hibernate docs.

Code:
public int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
      {
         if(((IEntity)entity).SaveRequested)
            return null;
         else
            return new int[] {};
      }


I found that it works fine as long as you call Save() or Update(), but not SaveOrUpdate(). Not sure why. Anyway, the net result was that no objects are saved other than the ones that I explicitly specify I want saved.

If you don't want your consumers to see the SaveRequested flag jsut implement it as an explicit interface.

Cheers,

Symon.[/code]


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.