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.  [ 9 posts ] 
Author Message
 Post subject: SQL-Query caching data and I cant disable it
PostPosted: Mon Feb 09, 2009 5:13 am 
Beginner
Beginner

Joined: Sat Jan 26, 2008 6:33 am
Posts: 24
Hello!

I'm using a named query to get statistical data. If the parameter avgMonth changes, there will be different values for the column Average. As far as I can see with Profiler, the query is properly sent to SQL-Server. Unfortunately NHibernate returns a collection with unchanged values. I tried all settings to disable caching, but no changes. Any suggestions?

The mapping file:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Finance.Core" namespace="REF.Finance.Core">
   <!-- the class definition -->
   <class name="BookingStatistic">
      <cache usage="read-only" />
      <composite-id>
         <key-many-to-one name="Account" column="Account" class="Account" />
         <key-property name="Date" column="Date" type="DateTime" />
      </composite-id>
      <property name="Amount" column="Amount" type="float" />
      <property name="Average" column="Average" type="float" />
   </class>
   <!-- query daily statistic values -->
   <sql-query name="BookingStatistic.Daily" cacheable="false" cache-mode="ignore" read-only="true" flush-mode="always">
      <return class="BookingStatistic" />
      <![CDATA[
      with source as
      (
         select      bkg_acc_id,
                     cast(cast(year(bkg_date) as nvarchar) + right('0' + cast(month(bkg_date) as nvarchar), 2) + '01' as datetime) as bkg_date,
                     bkg_amount
         from         booking
         where         bkg_acc_id = :account
         and         bkg_date >= dateadd(month, -:avgMonth, :fromDate)
         and         bkg_date <= :toDate
      ),
      aggregate as
      (
         select      bkg_acc_id,
                     bkg_date,
                     sum(bkg_amount) as bkg_amount
         from         source
         group by      bkg_acc_id,
                     bkg_date
      ),
      movingAverage as
      (
         select      bk1.bkg_acc_id,
                     bk1.bkg_date,
                     avg(bk2.bkg_amount) as Average
         from         aggregate as bk1
         inner join   aggregate as bk2
         on            bk2.bkg_acc_id = bk1.bkg_acc_id
         and         bk2.bkg_date >= dateadd(month, -:avgMonth, bk1.bkg_date)
         and         bk2.bkg_date <= bk1.bkg_date
         group by      bk1.bkg_acc_id,
                     bk1.bkg_date
      )
      select      bk1.bkg_acc_id         as Account,
                  bk1.bkg_date         as Date,
                  bk1.bkg_amount         as Amount,
                  bk2.Average            as Average
      from         aggregate as bk1
      inner join   movingAverage as bk2
      on            bk2.bkg_acc_id = bk1.bkg_acc_id
      and         bk2.bkg_date = bk1.bkg_date
      where         bk1.bkg_date >= :fromDate
      and         bk1.bkg_date <= :toDate
      order by      bk1.bkg_date
      ]]>
   </sql-query>
</hibernate-mapping>


The loading function:

Code:
public void Load(Account account, DateTime fromDate, DateTime toDate, int avgMonth)
{
   IQuery query = Database.OpenSession().GetNamedQuery("BookingStatistic.Daily");
   query.SetParameter("account", account);
   query.SetInt32("avgMonth", avgMonth);
   query.SetDateTime("fromDate", fromDate);
   query.SetDateTime("toDate", toDate);
   m_Collection = query.List<BookingStatistic>();
   OnListChanged(ListChangedType.Reset, -1);
}


Greetings!
Zorgoban


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 7:53 pm 
Beginner
Beginner

Joined: Sat Jan 26, 2008 6:33 am
Posts: 24
Ok, I solved this one with adding the values to the key. But I'm also doing a big update statement on the bookings and now I'm looking for a solution to force Hibernate to update the cache on those. The function that executes the statement looks like this:

Quote:
public static void MatchUnassignedBookingContacts()
{
// get session reference
ISession session = Database.OpenSession();
// load and execute query to match new bookings
IQuery query = session.GetNamedQuery("matchUnassignedBookingContacts");
query.ExecuteUpdate();
}


This is a static function of the BookingCollection class. Database is a static class that handles configuration and the current session.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 8:14 pm 
Beginner
Beginner

Joined: Sat Jan 26, 2008 6:33 am
Posts: 24
Ok, I can call session.Clear() to get the desired result. Can anyone think of a better way to only remove the bookings from the cache?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 12:53 pm 
Beginner
Beginner

Joined: Wed Dec 10, 2008 5:59 am
Posts: 47
Even though you've tried all sorts of solutions, have you tried setCacheMode()?

By setting currentSession.setCacheMode(CacheMode.REFRESH), the cache will be updated with what you load from the database - but Hibernate won't read any values from the Cache.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 19, 2009 2:09 pm 
Beginner
Beginner

Joined: Sat Jan 26, 2008 6:33 am
Posts: 24
Thx for your reply. Unfortunately this does not work. As far as I understand, this property affects 2nd level cache. My problem seems to be 1st level cache. What I really dont understand is, that NHibernate actually does perform the query but is not able to recognize the changes in the objects. I really have to force it to empty its cache first. Smells like a bug, but I didnt dig the sourcecode yet. Also it doesnt look like any NHibernate developer is reading the boards here.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 1:06 pm 
Newbie

Joined: Mon Mar 02, 2009 12:45 pm
Posts: 2
I have the same problem...
Did you solve it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 5:31 am 
Beginner
Beginner

Joined: Sat Jan 26, 2008 6:33 am
Posts: 24
Zorgoban wrote:
Ok, I can call session.Clear() to get the desired result. Can anyone think of a better way to only remove the bookings from the cache?

Yes, I call session.Clear() after doing the update on the bookings, so I get new results the next time they're getting queried. Since there did not any developer answer to this, I'm wondering if NHibernate is still developed at all. Is there some serious, free alternative to NHibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 11:20 am 
Newbie

Joined: Mon Mar 02, 2009 12:45 pm
Posts: 2
Zorgoban wrote:
Zorgoban wrote:
Ok, I can call session.Clear() to get the desired result. Can anyone think of a better way to only remove the bookings from the cache?

Yes, I call session.Clear() after doing the update on the bookings, so I get new results the next time they're getting queried. Since there did not any developer answer to this, I'm wondering if NHibernate is still developed at all. Is there some serious, free alternative to NHibernate?



Oh, i c... I call session.Evict to remove specific object from cache... But anyway i think nhibernate have a better way to do this...



Yep, i think it still developed (see link below)
http://sourceforge.net/project/stats/detail.php?group_id=73818&ugn=nhibernate&type=svn


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 05, 2009 3:48 am 
Beginner
Beginner

Joined: Sat Jan 26, 2008 6:33 am
Posts: 24
Unfortunately I cannot use Evict, since I dont have the instances of the previously queried Bookings available during the procedure call. The session factory has a function to evict specific types, but that didnt work either. Documentation says its for lv2 cache, i suppose my objects are in lv1 cache.


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