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