NHibernate 3.0: TransactionScope and Auto-Flushing

后端 未结 4 711
一生所求
一生所求 2021-01-02 18:07

In NHibernate 3.0, FlushMode.Auto does not work when running under an ambient transaction only (that is, without starting an NHibernate transaction). Should it?

相关标签:
4条回答
  • 2021-01-02 18:12

    The answer provided by Diego does not work in the case where you have an oracle database. (releated question). The session.BeginTransaction will fail because the connection is already part of a transaction.

    Looks like we have to write some code around this problem in our application (WCF,NHibernate, Oracle), but it just feels like something that NHibernate should provide out of the box. So if anyone has a good answer, it would be really appreciated.

    0 讨论(0)
  • 2021-01-02 18:19

    Good news. Thanks to Jeff Sternal (who nicely identified the problem) I updated https://nhibernate.jira.com/browse/NH-3583 and thanks to the NH staff, there's already a fix and a pull request so in the upcoming release 4.1.x.x this ISSUE will be fixed.

    0 讨论(0)
  • 2021-01-02 18:26

    For me, I don't know the reason behind, but by forcing session flush before session is disposed seemed to worked for me. e.g. using(session) { //do your work

    session.Flush(); }

    I verified that this works with my distributed transaction, since without doing this, I always get "transaction has aborted" when TransactionScope is disposed.

    Even set session.FlushMode to Commit did NOT work for me.

    0 讨论(0)
  • 2021-01-02 18:29

    You should use an explicit NHibernate transaction always.

    using (TransactionScope scope = new TransactionScope()) 
    using (ISession session = sessionFactory.OpenSession())
    using (ITransaction transaction = session.BeginTransaction())
    {
        //Do work here
        transaction.Commit();
        scope.Complete();
    }
    

    I see you also wrote in the NH dev list - while this can change in the future, that's how it works now.

    0 讨论(0)
提交回复
热议问题