NHibernate complex object mapping? Lazy Loading is the rescue

In my current project, we have a complex object model relationship . As and when the development progress we mapped our relationships one by one and the application was performing well and in due course of time the relationship graph started growing, we started finding poor performance in terms of response time. We analysed the sql created by Nhibernate and found that almost entire database is pulled because of almost all tables are related including masters.

When our team complaint about performance, I thought it is due to master tables because we have many masters and every time we pull information from masters. I told them do not worry about performance, we will enable 2nd level cacahe for masters. Nhibernate SQL revealed that the culprit is SQL joins with almost all tables in our DB.

The rescue is, we enabled Lazy Loading to all our classes. Now the application is performing well within the expected  query execution time.

Note: While enabling Lazy Loading, be careful about caching the objects because the session will not be available for those related classes so you will receive Nhibernate session does not exist error. In such situation, do eager loading.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s