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.  [ 6 posts ] 
Author Message
 Post subject: Question about 2nd level cache
PostPosted: Thu Sep 29, 2005 3:09 pm 
Beginner
Beginner

Joined: Mon Sep 26, 2005 5:57 pm
Posts: 39
I am in the process of adding second level cache to my ASP.Net project.

I have noticed that only the objects retrieved by calling Get or Load on the primary key are stored in the 2nd level cache. If I query an object by any of its properties, the object is not stored in the cache.

Is the behaviour that I am noticing, in fact, true? Or is it something that I am doing incorrectly.

If it is true, may I request a feature addition, to add support for caching objects even if retreived by query on properties.

Thanks

Arvinder Chopra


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 3:24 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Objects should be added to the cache even when loaded through a query. Try enabling debug log, you should see the relevant messages in the log.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 9:44 pm 
Beginner
Beginner

Joined: Mon Sep 26, 2005 5:57 pm
Posts: 39
I have verified that the object is not getting cached when using a Session.CreateCriteria

Here is the mapping file of the class Task:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo" default-cascade="save-update">
  <class name="CID.IPath.NewBusinessLayer.Task, CID.IPath.NewBusinessLayer" table="Task">
    <cache usage="read-write" />
    <id name="Id" type="Int32" column="TaskId">
      <generator class="identity" />
    </id>
    <bag name="roles" table="role_task_association" inverse="true" lazy="true">
      <cache usage="read-write" />
      <key column="task_id" />
      <many-to-many class="CID.IPath.NewBusinessLayer.claimuserrole, CID.IPath.NewBusinessLayer" column="role_id" />
    </bag>
    <property name="TaskDescription" column="TaskDescription" type="String" />
    <property name="TaskName" column="TaskName" type="String" />
  </class>
</hibernate-mapping>


Here is some test code I wrote in ASP.Net:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim task As Task = task.Get(GetType(Task), 4)
        Label1.Text = task.TaskDescription

    End Sub


When I hit Refresh a couple of times, I only see one SQL statement executed in the SQL profiler:
Code:
exec sp_executesql N'SELECT task0_.TaskId as TaskId0_, task0_.TaskName as TaskName0_, task0_.TaskDescription as TaskDesc2_0_ FROM dbo.Task task0_ WHERE task0_.TaskId=@p0', N'@p0 int', @p0 = 4


Whereas if I change the code to something like this:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim task As Task = task.Get(GetType(Task), "TaskName", "Claim Editing")
        Label1.Text = task.TaskDescription

    End Sub


which underneath is using Session.CreateCriteria, I see the following statement executed every single time I hit Refresh:
Code:
exec sp_executesql N'SELECT this.TaskId as TaskId0_, this.TaskName as TaskName0_, this.TaskDescription as TaskDesc2_0_ FROM dbo.Task this WHERE this.TaskName = @p0', N'@p0 nvarchar(4000)', @p0 = N'Claim Editing'


What do you think?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 29, 2005 9:59 pm 
Beginner
Beginner

Joined: Mon Sep 26, 2005 5:57 pm
Posts: 39
In another post, you had written that the @nd level cache works only for Gets:

sergey wrote:
The second-level cache only works for session.Load/Get and session.Enumerable, so try changing the Find("FROM Division...") line to s.Get(typeof(Division), d.ID);


Has this changed since?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 3:53 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
There are two things you usually do with a cache: one is you store objects in it, and the other is you look up objects in it. Objects get stored in the cache every time they are loaded (by ID, by a query, by criteria, etc.), but they only get looked up in the cache when loaded by ID (Get/Load/Enumerable), since it's impossible to know their ids otherwise.

If you want to cache query results, this is also possible using the query cache. I don't remember now if it works for criteria, for HQL queries just use IQuery.SetCacheable(true) (and enable the cache in the configuration).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 01, 2006 3:49 am 
Newbie

Joined: Wed Mar 29, 2006 2:24 am
Posts: 14
i am using NHibernate 1.0.2 and the HQL query result caching is not working (and so is the caching through Criteria). it it really supposed to work in this version?

thanks!

_________________
http://devpinoy.org/blogs/joeycalisay


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