Saturday, November 6, 2010

NHibernate Hidden Gems - PainIsWeaknessLeavingTheBody

This code is pulled directly from Ayende's presentation at NHDay in Bologna. I would just link there, but this code only exists in the video (no source was provided).

Ayende provided an Interceptor implementation to make developers feel the network latency pain along with catching too many queries per session.

public class PainIsWeaknessLeavingTheBody: EmptyInterceptor
public override SqlString OnPrepareStatement(SqlString sql)
// check for too many queries
var count = (int)(HttpContext.Current.Items["nh_statment_count"] ?? 0)
if (++count > 30)
throw new InvalidOperationException("Stop querying the DB, dummy!");

HttpContext.Current.Items["nh_statment_count"] = count;

// add a sql delay
return new SqlString("WAITFOR DELAY '00:00:01'\r\n").append(sql);

All you have to do from here is pass in a new PainIsWeaknessLeavingTheBody to your call to OpenSession.

Some of you may think this is crazy. And as Ayende points out why in the world does SQL Server have a sleep implementation?!? I really like the idea for both tracking the number of queries per session along with adding a delay to make sure your customers aren't having to wait excessively.


Patricia Howell said...

It is the best time to make a few plans for the long run and it is time to be happy. I've learn this publish and if I could I want to suggest you few fascinating things or tips. Perhaps you could write next articles referring to this article. I desire to read more things about it! yahoo email login

Post a Comment