-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Counting the number of DB queries
PostPosted: Fri Dec 07, 2007 3:59 pm 
Newbie

Joined: Thu Oct 26, 2006 4:02 pm
Posts: 12
Hi all,

At DevTeach in Vancouver I heard someone say that there is a way to restrict, or at least count the number of DB calls that NHibernate generates within a session. Does anyone know where I can find this functionality?

Thanks,
Matt


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 09, 2007 2:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
I am not sure about restricting the number of DB calls. However, NHibernate.Statistics is implemented in the current trunk. You may use that to count the DB calls. With NHibernate 1.2 and before, you pretty much have to tap into the logging stream to do that. In some of the NHibernate tests, they use the following class to achieve this; adapt for your own need:
Code:
using System;
using System.Text;
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Repository.Hierarchy;

namespace NHibernate.Test
{
   /// <summary>
   /// A disposible object that taps into the "NHibernate.SQL" logger and
   /// collection the log entries being logged.  This class should be used
   /// with a C# using-statement
   /// </summary>
   public class SqlLogSpy : IDisposable
   {
      private Logger sqlLogger;
      Level prevLogLevel;
      private MemoryAppender appender;

      public MemoryAppender Appender
      {
         get { return appender; }
      }

      public SqlLogSpy()
      {
         ILog log = LogManager.GetLogger("NHibernate.SQL");
         sqlLogger = log.Logger as Logger;
         if (sqlLogger == null)
            throw new Exception("Unable to get the SQL logger");

         // Change the log level to DEBUG and temporarily save the previous log level
         prevLogLevel = sqlLogger.Level;
         sqlLogger.Level = Level.Debug;

         // Add a new MemoryAppender to the logger.
         appender = new MemoryAppender();
         sqlLogger.AddAppender(appender);
      }

      public void Dispose()
      {
         // Restore the previous log level of the SQL logger and remove the MemoryAppender
         sqlLogger.Level = prevLogLevel;
         sqlLogger.RemoveAppender(appender);
      }
   }
}

Example usage of the SqlLogSpy class:
Code:
            using (SqlLogSpy spy = new SqlLogSpy())
            {
               Employee emp1 = sess.Get<Employee>(employeeId);

               Assert.AreEqual(1, spy.Appender.GetEvents().Length,
                               "Only one SQL statement should have been issued.");
            }

_________________
Karl Chu


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.