Getting Ado Connection From Nhibernate

I had a problem where I needed to use the sqlBulk to insert multiple records from a data table to database. Now we are using NHibernate but sometimes don't use it or the NHibernate entities. Instead, sometime we use Sql directly, either in a new ado connection or by using the NHibernate session.

We had a module which was using a mixture of some read data which was done using the NHibernate session, and some bulk inserts which were done in a new ado connection. Obviously that was a bug, so I thought of fixing it to use NHibernate throughout.


Use the ado connection do everything in one transaction and commit.

Use NHibernate session and do everything in that, so that if this module is called from another place it could use the same session.


Now as you can see from option 2, you can easily spot if another module is calling this module using its own session (say NHibernate), and the ado won't respect that and it will open a new connection. If the caller module is not using NHibernate then it should inject the connection down the line.


I am not saying that I picked the best solution but I decided to use NHibernate session and sqlBulk using connections from NHibernate connections. So, all the things went smoothly until I hit two walls

  1. A piece of code using an ado connection and a transaction
  2. Sql bulk copy

So for the first one I need a connection from NHibernate Session and a transaction. To get ado connection from NHibernate session this helped me

  1. var reliableConnection = (ReliableSqlDbConnection)yourUnitOfWorkOject.Session().Connection;  
  2. var adoConnFromSession = reliableConnection.ReliableConnection.Current;   

And it’s perfectly ok if you don’t want any transient error handling but if you do want that then this won’t give you any as by using the connection this way you sacrificed the transient handling. But I fixed that using this code

  1. _retryPolicy.ExecuteAction(() => {  
  2.     tryPersist(youObject);  
  3. });   

Off course you have to define SqlAzureTransientErrorDetectionStrategyWithTimeout yourself.

The other sub problem was to get a transaction. Now you can’t have a beginTransaction on the connection, as you can’t create parallel transaction using the NHibernate connection. So what you have to do is enlist your command in ITransaction from the session so this should work like this

  1. using(var cmd = new SqlCommand("select * from table", adoConnFromSession, null)) {  
  2.     yourCurrentUnitOfWork.Session().Transaction.Enlist(command);  
  3.     using(var reader = command.ExecuteReader()) {  
  4.         Other code  
  5.     }  
  6. }   

I used the same strategy in the bulk which was like this before my fix

  1. using(var cmd = new SqlCommand()) {  
  2.     using(var trans = adoCon.BeginTransastion()) {  
  3.         using(var bulk = new SqlBulkCopy(adoCon, SqlBulkCopyOptions.Default, trans)) {  
  4.             bulk.DestinationTableName = "tableName";  
  5.             bulk.WriteToServer(dt);  
  6.         }  
  7.         trans.Commit();  
  8.     }  
  9. }  
  10. }   

And after my fix it looked like this

  1. using(var cmd = new SqlCommand()) {  
  2.     youtUnitOfWork.Session().Transaction.Enlist(cmd);  
  3.     using(var bulk = new SqlBulkCopy(adoConnFromSession, SqlBulkCopyOptions.Default, cmd.Transaction)) {  
  4.         bulk.DestinationTableName = "tableName";  
  5.         bulk.WriteToServer(dt);  
  6.     }  
  7. }