0

I have this situation: I'm developing a software with Spring.net and Fluent NHibernate, and I noticed something very unusual with NHibernate's behavior. I really don't know whether it's correct or not, but for my software it cannot happen. I noticed that NHibernate is calling twice all the getters, even when I'm updating something on the database. For exemple, when I try to insert some values on dabatase, NHibernate gets the value from the property to put on database, and then it triggers a new get again, which I don't know why and how is called. I think it tries to retrieve the value from cache to compare both values (database and cache) and save the updated value on cache.

By the way, for some entities I have a secondary cache level, but this situation happens with entities that have and don't have this secondaty cache level.

Any help? Thank you.

Only a Curious Mind
  • 2,807
  • 23
  • 39
  • Frankly, if you design your getters to be slow and/or to have side-effects, I think you are begging for trouble. It's not really what a getter is meant to do. – Oskar Berggren Sep 18 '13 at 08:25

2 Answers2

0
  1. You should be able to find it out using print of current stack track in particular getter and compare it to other traces. This should show you starting point.

  2. What get into my mind:

    1. Backward get for caching because of second level cache as it stores properties of entities only.
    2. What about has-code computing? Is it use properties to or fields?
    3. Verification whether object in session is as same in the database.
Martin Podval
  • 1,097
  • 1
  • 7
  • 16
0

If with get you mean NHibernate runs a select on the database to retrieve the object, within an insert/update this will be done only if you provide the ID of the object and the object is not already available within the session.

If you don't have the object in the database and want NHibernate to insert a new one, do not provide an ID and configure the generator of your ID map correctly

Otherwise make clever use of proxies and prevent NHibernate to retrieve the full object by using Load<T> which will not actually hit the database but puts a proxy into the current session scope.

MichaC
  • 13,104
  • 2
  • 44
  • 56