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