Sunday, March 29, 2009

Nhibernate Update Performance Issue

I have been developing a project using NHibernate as the DAL layer. During the development, I have found an issue rather bizarre. When the application tries to run a long transaction with thousands of transactions, the performance is getting unbearable. When I debug the program, I found that the first few updates are very fast. But after a certain amount of updates (roughly 100), a single update is about 40 times slower. After some research, the issue is related to the way Hibernate manager its memory. Every update in Hibernate is cached in the memory until the session is flushed. So the solution is rather simple, every 20 updates, you can call the following commands to clear the cache.
_session.Flush();
_session.Clear();
(_session is an instance of ISession.)

1 comment:

  1. how did you find out that hibernate memory management was the reason?

    ReplyDelete