views:

103

answers:

2

Hi,

I have a situation where I am using NHibernate in a WCF service and using a TransactionScope for the transaction management. NHibernate enlists in the ambient transaction fine, but, any changes I make and save inside the transaction, are not visible to any queries I make while still in that transaction. So if I add an entity and session.save() it, then further on in the code, there is a linq query against that entities table, the entity I just added is not returned.

Strangely this seems to work fine if I use explicit NHibernate transactions in my tests.

Anyone have any ideas as to why and what I can do about it?

Many thanks

Andrew

A: 

Ok, seems like you have to call session.Flush after each and every update if using transactionscope, otherwise the queries don't pick up the changed data.

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

Andrew
A: 

@Andrew,

I was ready to answer you that the problem was the combination of your FlushMode and the HiLo generator, but simulating the problem I got the same problem as you.

Give me some data:

  1. Are you using Hilo, Assigned or any other kind of code managed generator, right?
  2. Are you setting your Session FlushMode to anything different of FlushMode.Auto?

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

The 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();
        }
    }

Regards,

Filipe

jfneis
Filipe, Yes I am using HiLo, and flushmode is auto. I'm using linq for the queries, not criteria, think that may be in part the cause.Thanks
Andrew