Blocking nested transactions NHiberate

I have an NHibernate transaction that does some work and calls an inherited method that uses DBTransactions to call multiple stored procedures. If any of these legacy method calls fail, the NHibernate transaction should rollback.

The problem is that the Legacy methods use the data in the database that the NHibernate transaction creates. This results in legacy SQL Process code blocking while awaiting Nhibernate. The Nhibernate process is waiting for the Legacy SQL process to terminate, causing a deadlock.

I tried using ReadUncommitted for both transactions, but it doesn't work.

I tried to set up a distributed transaction using System.EnterpriseServices as recommended in NHibernate in action, but that doesn't work either.

I'm not really sure where to go from here.

0


a source to share


1 answer


You can do one of several things in the order of preference you want.



  • If your NHibernate job comes first (in terms of execution order), then you should use your legacy methods to connect to the NHibernate database. ISession provides a Connection property that you can pass to your legacy ADO code.

  • Or vice versa, if it's easier. As long as they are using the same connection, you will be able to perform both sets of data access in the same transaction context. ISessionFactory.OpenSession () has an overload that accepts an IDbConnection instance as well.

  • Run an ambient implicit transaction before running legacy or NHibernate methods. When you're done with your Nhibernate and legacy code, you can transfer both of them at the same time by performing a transaction using an external transaction. (I only put this 3rd because it will probably take you longer than 1 or 2). See MSDN for how to do this.

  • If you are really stuck and using SQL Server you can do this, but it is really nasty. You can combine two separate connections into one database transaction. I can't find a link on how to do this, but post an answer if you're disgusting enough to give it a try!

+1


a source







All Articles