Question

I have a WCF MSMQ service (hosted in a windows service). My method has the TransactionScopeRequired attribute on it.

I am using Nhibernate to save my data to my database. I want to make sure I close each Nhibernate session after each call.

I was using the following approach (using the castle facility) in my data access

using(var session = sessionManager.OpenSession())
using(var transaction = session.BeginTransaction())
{

 // do work
transaction.Commit()

}

But when my main service method exits I am getting an error because I have already disposed of the Nhibernate session and I think the DTC needs this to do its commit.

My question is:

What would be the best way to close the Nhibernate session - after the DTC has committed (i.e after i have exited my service method?).

Thank you.

Was it helpful?

Solution

If you wrap your code in the following

using (TransactionScope sc = new TransactionScope(TransactionScopeOption.Suppress)) 
{ 
     // code here 
     sc.Complete(); 
} 

Then NHibernate will not enlist in the ambient transaction and therefore DTC will not have a dependency on the database transaction.

This is a hunch as you haven't supplied the error details in your question.

EDIT

Of course by following this advice your database commit will not be performed under the same transaction as the dequeue action so if there is a failure in your database this may or may not cause the dequeue transaction to roll the message being processed back onto the queue, so you risk dropping messages in this way. You can compensate for this in various ways or you can just run the risk if the cost of dropped messages are not high.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top