{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

How to Avoid Throttling SQL Azure Database with NHibernate

Published by

Lately I had one weird experience on with running NHibernate on Windows Azure and I think it’s worth to share with you. NHibernate is matured industry-level ORM that is proven itself over years. Although it is not so easy to use as Entity Framework it is still my favorite due to flexibility and wide databases support it provides. In this posting I will show you one important trick with enum mappings. When ignored you may put huge unnecessary load to your database.

We have one system launched to public where NHibernate is used as ORM. The system runs on Windows Azure (web roles and one worker role). One nice day out worker role that runs bunch of services ran into troubles with database. From logs we found out that throttling protection was activated on SQL Azure.

It’s possible to switch to some bigger database package on SQL Azure but as we don’t have this much data yet and the site is not heavy load I got very interested in what is going on. Why is our service shooting something expensive to database?

Problem with enum mapping

When analyzing the problem I found out the problem. Take a look at the following NHibernate mapping:

<property name="AlertType" column="alert_type" type="int" />

The problem is in this mapping – it defines enumerator as integer. As enums and ints can be casted from one to another, NHibernate doesn’t complain about this mapping but internally it doesn’t like it. This mapping screws something up in change tracking and NHibernate thinks that entity is changed. Yes, it loads entity and finds then out it is changed. This means that when session is flushed a load of update commands are executed on database and as the result SQL Azure throttling protection may activate.

Mapping enums correctly

When mapping enums we have to specify also their type. This mapping here is correct:

<property name="AlertType" column="alert_type">

  <type name="MyProject.MyEnum, MyProject"></type>


After fixing all enum mappings to use correct enum types we calmed down NHibernate and this far we have had no problems with SQL Azure throttling anymore.

The post How to avoid throttling SQL Azure database with NHibernate appeared first on Gunnar Peipman - Programming Blog.


Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB.

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks