NHibernate with TransactionScope

前端 未结 6 1525
闹比i
闹比i 2020-12-02 18:25

Can anyone give me a quick overview of using TransactionScope with NHibernate? Do I need to do anything special with the session/IEnlistmentNotification/etc. to get this to

6条回答
  •  Happy的楠姐
    2020-12-02 19:23

    I have been using nHibernate 2.1 for awhile, and after a few production issues and trying quite a few variations, we have settled on the following method, as per Avoiding Leaking Connections With NHibernate And TransactionScope:

            using (var scope = new TransactionScope(TransactionScopeOption.Required))
            {
                using (var session = sessionFactory.OpenSession())
                using (var transaction = session.BeginTransaction())
                {
                    // do what you need to do with the session
                    transaction.Commit();
                }
                scope.Complete();
            }
    

    As we are using MSMQ and WCF so we had to use the ambient transaction.

    We found that not using session.BeginTransaction() caused a connection leak. We also found that re-using a session after committing a transaction caused a race condition (nHibernate is not thread safe and DTSC Commits/Rollbacks occur on a background thread).

提交回复
热议问题