Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MiniProfiler's database profiling with NHibernate

What's the simplest way to use MiniProfiler's database profiling with NHibernate? In order for the profiler to work, I need to wrap the DbConnection that NHibernate uses in a ProfiledDbConnection.

I'm not too familiar with the internals of NHibernate, so I don't know where all the extensibility points are. (I noticed that an NHibernate ISession has a Connection property, but it is read-only.)

like image 487
Brant Bobby Avatar asked Jun 16 '11 15:06

Brant Bobby


1 Answers

[UPDATE] Please see the following links for a version of that uses RealProxy to proxy the SqlCommand - batching is now supported

  • blog http://blog.fearofaflatplanet.me.uk/mvcminiprofiler-and-nhibernate-take-2
  • gist https://gist.github.com/1110153

I've left the original answer unaltered as it was accepted. [/UPDATE]

I've managed to partially get this to work by implementing a Profiled Client Driver (example for Sql Server 2008 below) - this works for simple examples, however I haven't yet found a solution for NH batching (which attempts to cast the command back to SqlCommand)

public class ProfiledSql2008ClientDriver : Sql2008ClientDriver {     public override IDbCommand CreateCommand()     {         return new ProfiledDbCommand(             base.CreateCommand() as DbCommand,              null,             MiniProfiler.Current);     }      public override IDbConnection CreateConnection()     {         return ProfiledDbConnection.Get(             base.CreateConnection() as DbConnection,              MiniProfiler.Current);     } } 
like image 159
Robert Milne Avatar answered Sep 29 '22 22:09

Robert Milne