NHibernate and System.Transactions – A Success
I was asked today how NHibernate plays with the new System.Transactions namespace. I wasn’t sure about the answer, so I wrote a quick test to find out.
I was not surprised to find out that it worked just fine out of the box (for SQL Server, that is).
Account acc1 = new Account(); acc1.Name = "foo"; using (ISession session = sessionFactory.OpenSession()) { session.Save(acc1); session.Flush(); } using (TransactionScope tx = new TransactionScope()) { using (ISession session = sessionFactory.OpenSession()) { Account acc2 = (Account)session.Load(typeof(Account), acc1.Id); acc2.Name = "1234"; session.Flush(); } } using (ISession session = sessionFactory.OpenSession()) { Account acc3 = (Account)session.Load(typeof(Account), acc1.Id); Debug.Assert(acc3.Name == "foo"); } using (TransactionScope tx = new TransactionScope()) { using (ISession session = sessionFactory.OpenSession()) { Account acc4 = (Account)session.Load(typeof(Account), acc1.Id); acc4.Name = "1234"; session.Flush(); } tx.Complete(); } using (ISession session = sessionFactory.OpenSession()) { Account acc5 = (Account)session.Load(typeof(Account), acc1.Id); Debug.Assert(acc5.Name == "1234"); } |
What is going on here:
- We create an account, and set its name to “foo”
- We then open a System.Transactions TransactionScope
- We modify the account we created, changing its name, and then save it to the database
- Because we did not call Complete(), the transaction is rolled back the moment we exit the using() statement
- We check that the transaction was indeed rolled back.
- We then open a second System.Transactions TransactionScope
- We modify the account and save it to the database
- We call Complete() on the transaction, which commits it.
- Outside the transaction, we check to make sure that the change was persisted.
One thing to note here, this is done using SQL Server as the backend, and utilizing the support in ADO.Net for auto enlisting in transactions. I don’t think that it will work on other providers, unless they support System.Transactions to the same level that SqlClient does.
Comments
Comment preview