-->
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.  [ 8 posts ] 
Author Message
 Post subject: SysCache query, issues, headaches
PostPosted: Tue Aug 16, 2005 5:37 pm 
Newbie

Joined: Sat Jun 11, 2005 1:56 am
Posts: 19
Hi All,

Just having a few issues with SysCache, I'm pretty sure its my ignorance on the issue but am having a bit of a hard time finding info on SysCache.

Anyways, for some reason it appears that the Cache is not working as I expect. If I make changes to the database (outside of NH) I would expect these changes to be ignored, at least for the default expiration period. Currently this is true during a single Session, but I assume this is a Session level cache. As soon as I close a session and start a new one the database gets re-read. From the documentation I have managed to find, the SysCache should only evict all objects when the SessionFactory is closed not the Session.

Thanks in advance,

Guido Tapia


A few details:
NH (And contrib) Version: 0.9
Mapping File:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="PicNet.RiskShield.DAL.Division, RiskShieldDAL" table="`Division`">
    <jcs-cache usage="read-write"/>
    <id name="ID" column="`DivisionID`" unsaved-value="0">
      <generator class="native"/>
    </id>
    <property name="DivisionName" column="`DivisionName`" type="String" not-null="true" length="100"/>
  </class>
</hibernate-mapping>


Test Fixture:
Code:
[Test]
public void SimpleTest()
{
  // Gets a handle on a new session
  ISession s = DataFacade.GetSession();         
  Division d = new Division("_Name");
  s.Save(d);

  string nameQuery = "SELECT DivisionName from Division WHERE DivisionID = " + d.ID;
  Assert.AreEqual("_Name", (string) SqlHelper.ExecuteScalar(connection_string, CommandType.Text, nameQuery));
  // Change the name in the DB (bypassing NH)
  SqlHelper.ExecuteNonQuery(connection_string, CommandType.Text, "UPDATE Division SET DivisionName = '_Name2' WHERE (DivisionID = " + d.ID + ")");
  Assert.AreEqual("_Name2", (string) SqlHelper.ExecuteScalar(connection_string, CommandType.Text, nameQuery));         

  // Reload the 'Division' object from the same session
  d = (Division) s.Find("FROM Division WHERE DivisionID = " + d.ID)[0];   
  Assert.AreEqual("_Name", d.Name);    // Pass
}

[Test]
public void SessionStartEndTest()
{
  ISession s = DataFacade.GetSession();         
  Division d = new Division("_Name");
  s.Save(d);
  Assert.GreaterThan(d.ID,  0);

  string nameQuery = "SELECT DivisionName from Division WHERE DivisionID = " + d.ID;
  Assert.AreEqual("_Name", (string) SqlHelper.ExecuteScalar(connection_string, CommandType.Text, nameQuery));
  // Change the name in the DB (bypassing NH)
  SqlHelper.ExecuteNonQuery(connection_string, CommandType.Text, "UPDATE Division SET DivisionName = '_Name2' WHERE (DivisionID = " + d.ID + ")");
  Assert.AreEqual("_Name2", (string) SqlHelper.ExecuteScalar(connection_string, CommandType.Text, nameQuery));         

  // Close the session
  DataFacade.EndASPSession();
  // Start a new Session (SessionFactory.OpenSession())
  DataFacade.StartASPSession();
  // Get a handle on the new session
  s = DataFacade.GetSession();
  d = (Division) s.Find("FROM Division WHERE DivisionID = " + d.ID)[0];
  // This fails, appears to have read value from the database
  Assert.AreEqual("_Name", d.Name); // Fail
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 2:16 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
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);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:03 pm 
Newbie

Joined: Sat Jun 11, 2005 1:56 am
Posts: 19
Hi Sergey,

I have tried both Load and Get, both fail the fixture. I have also tried flushing the session before changing the database manually, hoping that this would save to the cache but again no caching.

The logs indicate that the SysCache is being initialised correctly for my entities and things appear to be getting cached.

One thing I have found that makes this fixture pass is if I call Session.Refresh on the object before modifying the DB.

However this (according to docs) causes another round trip to the DB so I want to avoid it. NH has the correct state of the obj so I dont need to refresh it right?

Thanks in advance

Guido Tapia


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:26 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Another thing (and this may well be a bug, but I'm not sure) - objects might not be cached on Save, only on Load/Get. So try Getting it once, closing the session, changing the field directly, and getting the object a second time.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:48 pm 
Newbie

Joined: Sat Jun 11, 2005 1:56 am
Posts: 19
No luck with that either (Load/Get to cache) . It appears to be only caching on Refresh(). However once refresh is called Get/Load and Enumerable work as expected.

Thanks

Guido


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:50 pm 
Regular
Regular

Joined: Tue May 24, 2005 12:55 pm
Posts: 56
If a refresh makes things work, try a lock (reconnect). No comm with the DB that I can see.

Code:
session.Lock(object, NHibernate.LockMode.None);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 5:25 pm 
Newbie

Joined: Sat Jun 11, 2005 1:56 am
Posts: 19
Nope, Lock does not help.

Tnx

Guido


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 22, 2005 5:46 pm 
Contributor
Contributor

Joined: Thu May 12, 2005 8:45 am
Posts: 226
I believe the session may be affecting this. As the System.Web.Caching namespace is web-based, the Session model may override the expiration times specified. Is closing the session the only trigger of this errant behavior?


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