http://knol.google.com/k/fabio-maulo/nhibernate-chapter-9/1nr4enxv3dpeq/12

> 9.4.3. Reattaching detached objects
>
> The Lock() method allows the application to reassociate an unmodified
> object with a new session.
>
> //just reassociate:
> sess.Lock(fritz, LockMode.None);
> //do a version check, then reassociate:
> sess.Lock(izi, LockMode.Read);
> //do a version check, using SELECT ... FOR UPDATE, then reassociate:
> sess.Lock(pk, LockMode.Upgrade);
>
> Also see http://nhforge.org/doc/nh/en/index.html for the original source.
Also look for mentions of "pessimistic locking".

Ted


On Thu, Jun 4, 2009 at 1:03 AM, MattF <[email protected]> wrote:

>
> can you explain what the lock method does and why it works?
>
> i saw someone using it but i didn't really know what the consequences
> are.
>
> thanks
>
> On Jun 4, 2:28 pm, Ted Gifford <[email protected]> wrote:
> > The Lock method on NHibernate Session is similar to what you're asking.
> > Don't think AR has that method, but you can always access the underlying
> > session.
> >
> > Ted
> >
> >
> >
> > On Wed, Jun 3, 2009 at 10:37 PM, MattF <[email protected]> wrote:
> >
> > > if anyone knows an answer to this, i would still very much like to
> > > hear it.
> >
> > > however i have just changed my web service to not return the AR Model
> > > objects.
> >
> > > so my update method now does this (psedocode):
> >
> > > Update(ThingyData data)
> > > {
> > >    Thingy thingy = GetThingyFromDB(data.Id);
> > >    UpdateThingyFromData(thingy, data);
> > >    UpdateThingyInDB(thingy);
> > > }
> >
> > > which is probably the "right" way to do it anyway, so that my client
> > > isn't tied to my DB Model
> >
> > > On Jun 3, 10:20 am, MattF <[email protected]> wrote:
> > > > what would be nice is if i could put some thing into it so that
> before
> > > > it goes to update any object, if it doesn't have the current state of
> > > > the object it goes and retrieves it from the DB in a way which would
> > > > mean that my postupdate has the OldState.
> >
> > > > this way my actual web method could stay how it is (free of any of
> > > > this logic)
> >
> > > > On Jun 3, 10:11 am, MattF <[email protected]> wrote:
> >
> > > > > This has probably been asked before, but i couldn't find a existing
> > > > > post explaining it.
> >
> > > > > Currently we have a web service which is accessed by our client
> > > > > application in order to talk to the database.
> >
> > > > > The asmx WebService has web methods similar to this (this is a
> > > > > simplification):
> > > > > NOTE: Thingy is an AR Model
> >
> > > > > [WebMethod]
> > > > > public Thingy GetThingy(int id)
> > > > > {
> > > > >     return ActiveRecordMediator<Thingy>.FindByPrimaryKey(id);
> >
> > > > > }
> >
> > > > > [WebMethod]
> > > > > public Thingy UpdateThingy(Thingy thingy)
> > > > > {
> > > > >     ActiveRecordMediator<Thingy>.Update(thingy);
> > > > >     return thingy;
> >
> > > > > }
> >
> > > > > there is also some other code in another part of the code which
> > > > > basically creates a SessionScope for each web request.
> >
> > > > > Now, this code seemed to work at first (updated the DB
> appropriately).
> > > > > However then i added an IPreUpdateEventListener which is basically
> > > > > used to log which properties on each object where changed.
> >
> > > > > The problem that i had is in the OnPostUpdate(PostUpdateEvent
> > > > > postUpdateEvent) method of my EventListener
> postUpdateEvent.OldState
> > > > > is always null.
> >
> > > > > After some research and asking questions i have worked out that
> this
> > > > > is because when calling the Update web method, the 'thingy' that is
> > > > > passed in is a detatched model (not attatched to the current
> session)
> > > > > so AR has no way of knowing the previous values of the Thingy
> (which
> > > > > makes sense).
> >
> > > > > however i need to find some way of updating the thingy in the
> > > > > database, but still being able to know which properties where
> changed
> > > > > in that row of the database.
> >
> > > > > I understand that i may have to do a FindByPrimaryKey before i do
> the
> > > > > update so that AR can know the state of the object before the
> update.
> > > > > but i couldn't get it to work by just doing a find and then an
> update
> > > > > on the object passed in as the parameter.
> >
> > > > > p.s. i realize i could probably get it to work by doing a Find,
> then
> > > > > updating the returned object manually from the object passed in
> (and
> > > > > then saving the object returned from the find)... but i don't want
> to
> > > > > have to update the object manually, because this is going to have
> to
> > > > > be done for many-many different models, so i need a method that is
> not
> > > > > fragile and can be done pretty simply for any type
> >
> > > > > thanks in advance
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to