NHibernate and interceptors - measuring/monitoring SQL round-trip times

前端 未结 1 1889
萌比男神i
萌比男神i 2021-01-20 18:49

In order to get early-warning of a slow or potentially slow areas, I\'d like to have an Interceptor for NHibernate that can act as a performance monitor, so that any databas

1条回答
  •  悲&欢浪女
    2021-01-20 19:15

    I had a similar problem. I wanted measure and log all queries that goes through NHibernate. What I did is I wrote a custom batching factory (in this case I work with oracle) but you can apply the same technique to any db:

    1-) Implement batcher factory, (in this case I am extending existing factory)

    public class OracleLoggingBatchingBatcherFactory : OracleDataClientBatchingBatcherFactory
    {
        public override IBatcher CreateBatcher(ConnectionManager connectionManager, IInterceptor interceptor)
        {
            return new OracleLoggingBatchingBatcher(connectionManager, interceptor);
        }
    }
    

    2-) Implement the Batcher itself (in this case I am extending existing batcher). Make sure you inherit IBatcher again since we want our new methods to be called

    public class OracleLoggingBatchingBatcher : OracleDataClientBatchingBatcher, IBatcher
    {
          .... // here override ExecuteNonQuery, DoExecuteBatch and ExecuteReader. 
               //You can do all kind of intercepting, logging or measuring here
               //If they are not overrideable just implement them and use "new" keyword if necessary
               //since we inherit IBatcher explicitly it will work polymorphically.
               //Make sure you call base implementation too or re-implement the method from scratch
    }
    

    3-) Register the factory via NHibernate config:

        OracleLoggingBatchingBatcherFactory, MyAssembly
    

    0 讨论(0)
提交回复
热议问题