k.c.
k.c.

Reputation: 1835

How to obtain an NHibernate session with a connection that has an active transaction

We are usin NHibernate as our DAL for the domain model. At the end of a bulk process I want to do some actions using the NHibernate DAL

Something like:

public void BulkUpdate()
{
   var connection =  new sqlConnection(“connectonstring”);
   var transaction = connection.BeginTransaction();
   // Do Bulk Stuff
   var session = SessionFactory.OpenSession(connection);
   var result = session.Query<DomainClass>();
   // Do Stuff with Result
   transaction.Commit();
   connection.Close();     
}

The read attempt of NHibernate fails, because it tries to use a command object, that has no transaction set. But I do not know of a way to pass the transaction object to it.

Any Nhibernate guru's out there?

Upvotes: 1

Views: 1554

Answers (1)

Sixto Saez
Sixto Saez

Reputation: 12680

I think this approach might work but don't have time right now to verify it:

  • First, create an NHibernate session from your connection string as shown in this SO answer
  • Open the transaction like this: session.BeginTransaction()
  • Use the session.Connection property to perform the bulk stuff
  • Complete the processing with the session and commit as appropriate

Upvotes: 3

Related Questions