you don't need to call session.Update().  you should also call
tx.Rollback() if there is an error.

using(var tx = session.BeginTransaction())
try
{
   session.Get<Product>(id);
   product.Name = ...
   tx.Commit();
}
catch
{
   tx.Rollback();
   throw;
}

if you are using a messaging framework, then you can move the
transaction management to the command/message module (or whatever the
name of it is.

when a message is received begin the transaction.
when a message is completed without error commit.
when a message is completed with error rollback.
when a message completes dispose of the transaction.

then you message handler code looks like this

session.Get<Product>(id);
product.Name = ...

clean, simple code.

another thing to consider... are you processing messages is batch? if
so you may be opening more sessions then necessary. this is the case
with Rhino.ServiceBus. You can manage if a session is open or not with
CurrentSessionContext.

CurrentSessionContext.Bind(factory.OpenSession())
CurrentSessionContext.HasBind(factory);
CurrentSessionContext.Unbind(factory).Dispose();

var sessionfactory.GetCurrentSession();


On Sep 10, 8:34 pm, Daniel Hölbling <[email protected]> wrote:
> I've just spent almost 3 hours debugging and still can't find a fault with
> my code.
>
> The reproduction looks like this:
>
> using (var tx = session.BeginTransaction())
> {
>     var product = session.Get<Product>(message.ProductDto.Id);
>     product.Name = message.ProductDto.Name;
>     product.PartNumber = message.ProductDto.PartNumber;
>     product.Price = message.ProductDto.Price;
>     session.Update(product);
>     tx.Commit();
>     Logger.InfoFormat("Updated Product {0}", product.Id);
>
> }
>
> I change some value of the entity in the UI, this code is run (over the
> wire) and the IsDirty() Method is set to False and no Update is generated.
> (Checked with NHProf..)
> I just re-checked and upon changing the value of product.PartNumber for 8
> times it didn't generate a UPDATE once.
>
> It gets significantly worse once I run this with distributed transactions
> from NServiceBus where I loose every second UPDATE to the database without
> any error or anything.. It just doesn't update the DB since it doesn't think
> the session is dirty.
>
> Is there anything I should be aware of?
>
> greetings Daniel

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" 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/nhusers?hl=en.

Reply via email to