0

This is an elaboration and clarification of this question.

Suppose I have two tables, Foo and Bar.

Bar has a FK to Foo.

In the application, the tables are represented by classes and Foo has a list of Bars. Bar has a property for the id of the Foo it has a FK to in the database.

In the context of a Session and Transaction with IsolationLevel.ReadUncommitted, I add an instance of Foo to the database, assign the generated id to the Foo_id property of an instance of Bar and also add it to the database.

Now, before calling Transaction.Commit(), is it possible to have NHibernate read out Foo with a list of Bar from the database? That is, read the data uncommitted?

I have created a VS2012 project which demonstrates this. It includes a SSDT project for building the required database and has tests showing what I asking about.

Thank you.

Community
  • 1
  • 1
hlintrup
  • 169
  • 1
  • 13

1 Answers1

0

In the context of the same transaction you can read back all changes you've made to the database, so yes. The isolation level of the transaction doesn't matter for changes made within that transaction.

However, NHibernate typically won't update an already loaded object. But if you Flush(), followed by either Clear() or Evict() and then read Foo again, its collection will include the instance of Bar. (As long as you are in the same session, committing the transaction is irrelevant to this, except that with default settings Commit() automatically calls Flush().)

Oskar Berggren
  • 5,583
  • 1
  • 19
  • 36
  • I have tested with Flush(), Clear() and Evict(). – hlintrup Aug 12 '13 at 08:17
  • When I read Foo again after Clear() or Evict(foo), its collection does include the instance of Bar. Flush() on its own has no effect. Why does Clear() produce the desired result? The doc says it evicts all instances and cancels any pending changes. – hlintrup Aug 12 '13 at 08:24
  • @hlintrup For that to happen, it means that NHibernate must have found a reason to write the change to the database. Flush() is only the manual method, it might happen at other times too. – Oskar Berggren Aug 13 '13 at 12:12
  • But why does calling Clear() make it possible for NHibernate to read changes to a loaded instance? The session should be completely empty if Clear() actually does evict all instances and cancels any pending changes, no? – hlintrup Aug 15 '13 at 06:39
  • @hlintrup Clear() does not make it possible to read changes to a loaded instance. If you request the entity after having called Clear() on the session, NHibernate will read the required data from the database and instantiate a new object. It will show the current state of the database. So for the change to be visible, something must have triggered Flush() before you called Clear(). On the other hand: If you retrieve an object, change a simple property, then immediately call Clear(), then retrieve the same entity, the new object instance will NOT reflect that change. – Oskar Berggren Aug 15 '13 at 07:24