-->
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.  [ 3 posts ] 
Author Message
 Post subject: Caching doesn't work - SqlServer is queried every time
PostPosted: Fri May 16, 2008 5:34 am 
Newbie

Joined: Thu Apr 10, 2008 8:53 am
Posts: 3
Hi all,

I set up NHibernate and everything seemed to work as it should, but then I ran SQL Profiler to verify that the data was being cached properly...

When I run the same query 10 times, the database gets hit all 10 times. Caching apparently doesn't work.

Any suggestions to what I'm doing wrong will be appreciated very much. Thanks in advance.

Hibernate version:
1.2

Mapping documents:

The mapping of the class I'm testing:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="ECS.DataAccessLayer" namespace="ECS.DataAccessLayer.DataObjects">
   <class name="AfdelingObject" table="Afdeling" dynamic-update="true">
      <id name="AfdelingsID" column="AfdelingsID" type="Int32" unsaved-value="0">
         <generator class="identity" />
      </id>
      <property name="AfdelingsNavn" column="AfdelingsNavn" type="String" length="50" />
      <property name="Adresse" column="Adresse" type="String" length="50" />
      <property name="KundeReferenceNr" column="KundeReferenceNr" type="String" length="50" />
      <property name="KundeID" column="KundeID" type="Int32" />
      <many-to-one name="KontaktPerson" column="KontaktPersonID" class="KontaktPersonObject" insert="false" update="false" />
      <many-to-one name="Kunde" column="KundeID" class="KundeObject" insert="false" update="false" />
   </class>
</hibernate-mapping>


hibernate.cfg.xml:

Code:
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Server=xxx;initial catalog=xxx;User Id=xxx;Password=xxx</property>
      <property name="show_sql">true</property>
      <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
      <property name="max_fetch_depth">0</property>
      <property name="connection.isolation">ReadCommitted</property>
      <property name="default_schema">NNS.dbo</property>
      <property name="hibernate.cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
      <property name="hibernate.cache.use_query_cache">true</property>
      <property name="expiration">600</property>
      <mapping assembly="ECS.DataAccessLayer" />
   </session-factory>
</hibernate-configuration>



The code that retrieves the data from the database:

Code:
ISession session = NHibernateHelper.GetCurrentSession();
IList list = session.CreateCriteria(typeof(AfdelingObject)).List();
return (ArrayList)list;


The class that provides the session:

Code:
namespace ECS.DataAccessLayer
{
    public sealed class NHibernateHelper
    {
        private const string CurrentSessionKey = "nhibernate.current_session";
        private static readonly ISessionFactory sessionFactory;

        static NHibernateHelper()
        {
            sessionFactory = new NHibernate.Cfg.Configuration().Configure().BuildSessionFactory();
        }

        /// <summary>
        /// Gets the current NHibernate session to work with.
        /// </summary>
        /// <returns></returns>
        public static ISession GetCurrentSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                currentSession = sessionFactory.OpenSession(new SaveOrUpdateInterceptor());
                currentSession.FlushMode = FlushMode.Commit;
                context.Items[CurrentSessionKey] = currentSession;
            }

            return currentSession;
        }

        public static void CloseSession()
        {
            HttpContext context = HttpContext.Current;
            ISession currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                // No current session
                return;
            }

            currentSession.Close();
            context.Items.Remove(CurrentSessionKey);
        }

        public static void CloseSessionFactory()
        {
            if (sessionFactory != null)
            {
                sessionFactory.Close();
            }
        }
    }
}


Name and version of the database you are using:
SqlServer 2000


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 5:49 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
Hi,
you have to set your Criteria cachable:

Code:
session.CreateCriteria(typeof(AfdelingObject)).SetCachable(true).List();


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 7:18 am 
Newbie

Joined: Thu Apr 10, 2008 8:53 am
Posts: 3
Thanks typhos, that did the trick :-)

/Kim


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.