Using NHibernate with existing ADO.NET transactions

My co-worker Matt Wrock wrote an excellent blog post about using ADO.NET transactions in conjunction with NHibernate transactions/sessions.  This was a problem our team encountered since we are beginning the arduous transition towards using NHibernate as our ORM.  We have lots of code that uses TransactionScope and ADO.NET and it really isn’t feasible to rewrite everything for NHibernate in one fell swoop. This leaves us in the state of needing our existing transaction database code to work well with NHibernate.  It took us several attempts to figure out a workable solution and in the end we came upon the idea the Matt Wrock describes in his post.

Now before I elaborate on the drama and adventure that accompanied this learning experience and some of the failures that ensued, I’ll quickly jump to the moral of this story for those of you that simply want to know what is the right way to get NHibernate transactions to participate in a transaction defined by TransactionScope. Simply stated: its all in the nesting. Instantiate a TransactionScope on the outside and and begin and commit/rollback an NHibernate transaction on the inside.

If you are in the same situation as we were I definitely recommend reading his post and grab the class he created that wraps TransactionScope.

 

One thought on “Using NHibernate with existing ADO.NET transactions

Comments are closed.