can an nhibernate transaction flush only changes committed to session?

Let's say I open an ISession from the SessionFactory.

For some limited time (not long) this session is alive and used. I fetch entities from the database using this session, and change some of the entities according to some complex business rules. Then I want to save only some of the changed entities.

So, naturally:

        ITransaction t = null;

        try
        {
            t = session.BeginTransaction();

            session.SaveOrUpdate(entity);

            t.Commit();
        }
        catch (Exception ex)
        {
            this.Logger.Error("Transaction failed.", ex);

            if (t != null)
                t.Rollback();

            throw;
        }
        finally
        {
            if (t != null)
                t.Dispose();
        }

However, in accordance with the NH documentation, when transaction commit is called, the session is flushed. This means that all the dirty entities are persisted, even those which I didn't pass to SaveOrUpdate. If some are not in a valid state (e.g. no longer have their not null fk set) everything blows up completely - even if the entities which I tried to save are valid.

Is there a way to tell NH to only save the entity that I give it?

Answers


You can call ISession.Evict to remove objects from the session. My memory is that this affects only the object passed as the argument, not the entire object's graph, so changes to child collections are not evicted.

My suspicion is that your unit-of-work is too long. It's unusual to make changes to an entity that you do not want persisted to the database.

Oh, and the SaveOrUpdate call is unnecessary.


Need Your Help

Does glSubTexImage block?

opengl synchronization textures

This is a question about syncronization in OpenGL. And the question is:

Price History Log query in SQL SERVER

sql sql-server sql-server-2008 tsql

I have three tables, one distinctly contains the items to be sold. one that contains the name of the supplier of the item. one that contains the transactions which houses the amount item was purcha...