NHibernate Linq queries do not return data saved in one transaction

I have a situation where I am using NHibernate on a WCF service and using TransactionScope to manage transactions. NHibernate completes the transaction using external transactions, but any changes I make and save inside the transaction do not show up in any requests I make while they are still in that transaction. So if I add an entity and session.save () then further in the code there is a linq query against this entity table, the object I just added is not returned.

Oddly enough, this works fine if I use NHibernate explicit transactions in my tests.

Anyone have any ideas why and what I can do about this?

Many thanks

Andrew

+2


a source to share


3 answers


Ok, it looks like you need to call session.Flush after every update when using a transaction, otherwise the requests won't receive the modified data.



This is not great, if you ask me, does anyone know a better way?

0


a source


@Andrew,

I was ready to tell you that the problem was the combination of your FlushMode and the HiLo generator, but to simulate the problem, I had the same problem as you.

Give me some data:

  • Are you using Hilo, Assigned, or some other code driven control generator, right?
  • Are you setting your FlushMode session to something other than FlushMode.Auto?

AFAIK, setting FlushMode to Auto should solve these cases (at least it solves with NH TX), but I've never used TransactionScope and NH before. Maybe this is some kind of known limitation?



unit test follows:

    [TestMethod]
    public void TransactionScopeFlushMode()
    {
        MappedEntity ent1 = new MappedEntity() { Name = "a" };
        MappedEntity ent2 = new MappedEntity() { Name = "b" };

        using (TransactionScope tx = new TransactionScope())
        {
            ISession session = _sessionFactory.OpenSession();
            session.FlushMode = FlushMode.Auto;

            session.Save(ent1);
            session.Save(ent2);

            IList<MappedEntity> ents = session.CreateCriteria<MappedEntity>().List<MappedEntity>();
            Assert.AreEqual<int>(2, ents.Count);

            tx.Complete();
        }
    }

      

Hello,

Philip

0


a source


Are your reads part of the same ISession as your pending writes? Have you read this ... http://nhibernate.info/doc/nh/en/index.html#manipulatingdata-flushing

0


a source







All Articles