I've been evaluating using nhibernate for the data-access layer to an ASP.NET 2.0 web-service app. I'm having issues with caching; instead of grabbing the objects in the second-level cache (syscache2), it's "hydrating" them using n "selects", then realizing n times that the "Item was already cached".
I've been reading the docs on using the second-level cache, but I think I'm still a bit confused. What does "hydrating" mean? Is the above behaviour by-design? Shouldn't it check the cache first, see whether the objects have expired (by time and/or perhaps by a Sql 2005 notification), and only if necessary hit the database (and, when it does need to refresh, should it do n-selects)?
How do I use the command from the web.config file (below), or is that done automatically? How can I be notified when an object is evicted from the cache (eg: Sql 2005 notifies the app a change occured)?
thanks!!
--craig
Hibernate version: 1.2
Mapping documents:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="StandardMix,DAL" table="standard_mixes" lazy="false">
<cache usage="read-write" region="MyCacheRegion" />
<id name="Id" column="id" type="int">
<generator class="assigned" />
</id>
<property name="Title" column="title" type="string" not-null="true" />
<property name="Genre" column="genre" type="string" not-null="true" />
<property name="Type" column="type" type="string" not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Name and version of the database you are using: Sql Server 2005
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
20:52:39.242 [9] DEBUG NHibernate.Loader.Loader - Hydrating entity: DAL.StandardMix#765
...
20:52:39.289 [9] DEBUG NHibernate.Loader.Loader - total objects hydrated: 96
20:52:39.289 [9] DEBUG NHibernate.Impl.SessionImpl - resolving associations for: [DAL.StandardMix#765]
20:52:39.289 [9] DEBUG NHibernate.Impl.SessionImpl - adding entity to second-level cache [DAL.StandardMix#765]
20:52:39.289 [9] DEBUG NHibernate.Cache.ReadWriteCache - Caching: DAL.StandardMix, MyServiceDAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null#765
...
20:52:43.637 [9] DEBUG NHibernate.Loader.Loader - Initializing object from DataReader: [DAL.StandardMix#765]
20:52:43.637 [9] DEBUG NHibernate.Loader.Loader - Hydrating entity: DAL.StandardMix#765
...
20:52:43.668 [9] DEBUG NHibernate.Loader.Loader - total objects hydrated: 96
20:52:43.668 [9] DEBUG NHibernate.Impl.SessionImpl - resolving associations for: [DAL.StandardMix#765]
20:52:43.668 [9] DEBUG NHibernate.Impl.SessionImpl - adding entity to second-level cache [DAL.StandardMix#765]
20:52:43.668 [9] DEBUG NHibernate.Cache.ReadWriteCache - Caching: DAL.StandardMix, MyServiceDAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null#765
20:52:43.668 [9] DEBUG NHibernate.Caches.SysCache2.SysCacheRegion - Fetching object 'NHibernate-Cache:MyCacheRegion:DAL.StandardMix, MyServiceDAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null#765@765' from the cache.
20:52:43.668 [9] DEBUG NHibernate.Cache.ReadWriteCache - Item was already cached: DAL.StandardMix, MyServiceDAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null#765
20:52:43.668 [9] DEBUG NHibernate.Impl.SessionImpl - done materializing entity [DAL.StandardMix#765]
20:52:43.668 [9] DEBUG NHibernate.Impl.SessionImpl - resolving associations for: [DAL.StandardMix#819]
20:52:43.668 [9] DEBUG NHibernate.Impl.SessionImpl - adding entity to second-level cache [DAL.StandardMix#819]
------
here's the config for my web.config file:
<sysCache>
<cacheRegion name="MyCacheRegion" relativeExpiration="25" priority="High" >
<dependencies>
<commands>
<add name="DAL.StandardMix"
command="SELECT [id],[title],[genre],[type] FROM [dbo].[standard_mixes]"
connectionName="MyDb"
connectionStringProviderType="NHibernate.Caches.SysCache2.ConfigConnectionStringProvider, NHibernate.Caches.SysCache2"
/>
</commands>
</dependencies>
</cacheRegion>
</sysCache>
|