We are currently using NHibernate for accessing our database.

We have database, which store a "configuration". We have the notion of "revision", meaning that every table in our database has a revision, and when we make any change(even a small one), every field in our database get duplicated(except the changes that will not be the same).

The goal is to be able to switch easily from one revision from another, and be able to delete one configuration and still be able to switch from an earlier or older revision.

It implies that when we change the configuration, we make a lot of writing in the database(and other application will have to read also it).

This steps can take a very long time(5-10 minutes, we have a lot of table), compared to 10-20 seconds to store it in xml.

After spending some time in analysis, we have the impression that NHibernate has to make a lot of reflection to map database to c# objects(using our hbm.xml files).

My questions:

  1. How does NHibernate read/write properties in every object, with reflection, right?
  2. Does it make reflection at every write, or is there some optimization(cache, ...?)
  3. Is there a possibility to avoid this "reflection"? Like having some class created/compiled on build(like it's possible with entity framework)?
  4. If I've a working NHibernate model, is there something I can do to "tune" the DB access, without changing the database?

Thank you very much for your help.

Was it helpful?


Before assuming it is relection. I would strongly recommend downloading (as a free trial at least) NHProf to see what is taking NHibernate so long. Or some other database profiler.

If I had to guess it was the number of database updates that is required here but I wouldn't guess about performance without getting some metrics first ;)

For instance it might be that you need to increase your batch size in NHibernate if you are doing a lot of small updates in one session, which can be done in your nh config file

<property name="adonet.batch_size">300</property>


You cannot avoir Reflection, but it is not a problem. NHibernate use a lot of Reflection to prepare and emit dynamic code. Generated code is faster than static code because msil allow you more thing than c#.

If implementation of reflection usage seems to be your issue you can juste extends NHibernate but write a byte code provider.

Personnaly, proxy generator, usertype and property accessors can be faster than builtin implementation.

Generally, performance issue caused by Nhibernate is often :

  • missing fetching
  • precision data causing unwanted update
  • bad mapping (structure or simply datatype)
  • bad database/datatable configuration
  • bad commit strategy (flush mode for exemple)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow