WCF with MSMQ DTC - Close NHibernate Sessions

I have an MSMQ WCF service (hosted on a Windows service). My method has a TransactionScopeRequired attribute.

I use Nhibernate to store my data in my database. I want me to close every Nhibernate session after every call.

I used the following approach (using a lock 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 logs out, I get an error because I already selected a Nhibernate session, and I think that DTC should do this to commit.

My question is:

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

Thanks.

+4
source share
1 answer

If you wrap your code in the following

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

NHibernate will then not participate in an external transaction, and therefore DTC will not be dependent on the database transaction.

This is a hunch since you did not provide details of the error in your question.

EDIT

Of course, following these tips, your database commit will not be performed within the same transaction as the dequeue action, so if your database crashes, it may or may not cause the dequeue transaction to roll the processed message to queue, so you run the risk of discarding messages this way. You can compensate for this in various ways, or you can just take a chance if the cost of the discarded messages is low.

+3
source

Source: https://habr.com/ru/post/1447603/


All Articles