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.  [ 2 posts ] 
Author Message
 Post subject: .NET TransactionScope and flushing - correct behaviour
PostPosted: Wed Aug 25, 2010 11:00 am 
Newbie

Joined: Wed Aug 25, 2010 10:37 am
Posts: 2
I am using a .NET transaction scope and running the following to verify transaction commits/rollbacks
Using 2.1 nhibernate talking to SQL server db.

Code:
var b1 = new Person {Email = "abc@a.com", Username = "art"};
using (var session = NHibernateSession.CurrentFor("MyDatabase"))
{
      session.Save(b1);
      session.Flush();
  }
//Writes b1 away to database

using (var tx = new TransactionScope())
{
     using (var session = NHibernateSession.CurrentFor("MyDatabase"))
     {
          var b2 = (Person)session.Load(typeof(Person), b1.Id);
          b2.Username = "art1234";
          session.Flush();
      }
       tx.Complete();
}

using (var session = NHibernateSession.CurrentFor("MyDatabase"))
{
     var b3 = (Person)session.Load(typeof(Person), b1.Id);
     Assert.IsTrue(b3.Username == "art");
}



If I leave the session.Flush() uncommented the transaction commits even if I have the tx.Complete() commented out - I was expecting the transaction scope to decide whether to commit those changes. If I comment out session.Flush() and don't set tx.Complete() then the transaction rolls back as expected.

Is this expected behaviour for the session.Flush to commit the transaction implicitly - this is not what I was expecting?
My guess is that session.Flush creates its own implicit transaction and commits it and ignores the current .NET transaction scope.

Thanks


Top
 Profile  
 
 Post subject: Re: .NET TransactionScope and flushing - correct behaviour
PostPosted: Wed Sep 01, 2010 2:10 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You probably need to wrap your code in a transaction to make this work:

ITransaction trans = session.BeginTransaction();
....
session.Save(...)

// don't flush
trans.Commit();

_________________
--Wolfgang


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