CodeSmith Community
Your Code. Your Way. Faster!

Confused by caching(?) behaviour

Latest post 12-11-2006 6:42 PM by binaryace. 13 replies.
  • 09-04-2006 6:39 AM

    • Elliveny
    • Top 500 Contributor
    • Joined on 09-04-2006
    • Posts 13
    • Points 305

    Confused by caching(?) behaviour

    Hi,

    I'm working with netTiers for the first time and have had a fairly smooth ride so far but have encountered a rather frustrating problem which I need help with.

    I'm using netTiers with an ASP.NET 2.0 project and to illustrate my problem I built a simple page. My entity is 'School' and so my page has a 'SchoolDataSource'.

    <data:SchoolDataSource ID="dsSchools" EnableCaching=false runat=server CacheDuration="0" EnableViewState="False"><DeepLoadProperties Method="IncludeChildren" Recursive="False"></DeepLoadProperties></data:SchoolDataSource>

    I add a Gridview using this datasource, displaying the id and name (schID and schName):

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="SchID" DataSourceID="dsSchools" AutoGenerateColumns="False" EnableViewState=false><Columns><asp:BoundField DataField="SchID" HeaderText="SchID" SortExpression="SchID" /><asp:BoundField DataField="SchName" HeaderText="SchName" SortExpression="SchName" /></Columns></asp:GridView>

    I then add a SQL Data Source and another gridview against the same table, here using a simple SQL query:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" SelectCommand="SELECT [schName], [schID] FROM [School]"></asp:SqlDataSource>

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="schID" DataSourceID="SqlDataSource1" EnableViewState="False"><Columns><asp:BoundField DataField="schName" HeaderText="schName" SortExpression="schName" /><asp:BoundField DataField="schID" HeaderText="schID" InsertVisible="False" ReadOnly="True" SortExpression="schID" /></Columns></asp:GridView>

    Finally I add a button to postback the page and cause it to redisplay the data, with the current datetime beneath it:

    <asp:Button ID="Button1" runat="server" Text="Button" /><br /><%=Now()%> 

    This simple page works fine until I update the data using a SQL Query Analyzer session. I update the name of a particular row using a randomise function:

    update School set schName='School '+cast(cast(rand()*10000 as integer) as varchar(10)) where schID=53
    select schName from School where schID=53

    If I run the above and then click the button on my page then I see the gridview which is bound to the Sql Data Source show the new schName value immediately. The gridview which is using the SchoolDataSource sometimes updates, but usually maintains the old value for a fair period of time (I've haven't been able to determine a pattern, sometimes its a few seconds, sometimes a number of minutes). Huh? [:^)]

    Why is this happening? As far as I know I have no caching active and so I don't understand why the gridview doesn't show the updated data. I can repeat this over and over again with the same result.

    More confusion is raised if I run SQL Profiler. Here I can see that the query behind the SchoolDataSource (and the SQL Data Source) is run everytime I press the button - as I would expect - but despite this the displayed data is still old. The profiler shows the following has been run each time:

    SELECT
    [schID],
    [schName]
    FROM
    dbo.[School]
    Select @@ROWCOUNT

    AND

    SELECT [schName], [schID] FROM [School]

    Can anyone explain why I'm seeing this behaviour? I hope so; I need this to work and right now I'm stuck doing a very simple thing; trying to display current data from the database.

    Please help...

    Barry.

    • Post Points: 5
  • 09-05-2006 4:10 AM In reply to

    • Elliveny
    • Top 500 Contributor
    • Joined on 09-04-2006
    • Posts 13
    • Points 305

    Re: Confused by caching(?) behaviour

    Hmmm... no replies as yet... but quite a views of my posting. I'd hoped this was a quick one to resolve too. Sad [:(]

    Has anyone else had problems like the one I described? It would help me to know I'm not alone here even if you can't give me a way to resolve the issue.

    I notice that there are nightly builds of netTiers 2.0. The changelog for the one I have indicates it is .netTiers v0.9.5, the last change entry is 08/08/2006. Should I be downloading later builds and trying these out before posting? Is my question one for the Enterprise Library forums perhaps? I'm not sure if the problem I have is caused by netTiers or Enterprise Library.

    I'd appreciate any comment/advise you can offer here.

    Many Thanks!

    • Post Points: 5
  • 09-05-2006 5:47 AM In reply to

    • Elliveny
    • Top 500 Contributor
    • Joined on 09-04-2006
    • Posts 13
    • Points 305

    Re: Confused by caching(?) behaviour

    Further to my last post, I tried the latest nightly build and I still see the same behaviour.... Crying [:'(]

    Frustrating!!

    • Post Points: 35
  • 09-05-2006 8:29 AM In reply to

    • tomek
    • Top 100 Contributor
    • Joined on 04-20-2006
    • Posts 47
    • Points 1,370

    Re: Confused by caching(?) behaviour

    I posted it as bug, but I do not know if it has been fixed yet.

    http://community.codesmithtools.com/forums/thread/15247.aspx

    I am waiting for this one to be fixed too.

    • Post Points: 35
  • 09-05-2006 9:39 AM In reply to

    Re: Confused by caching(?) behaviour

    The behavior you are seeing is that the application is using EntityTracking, which basically is an in memory object cache for as long as there is a reference to the entity.  This is so that you don't create unnecessary new objects everytime the request for that object exists.  You get varied lengths of the cached entity because it's not until there are no more references to that entity, that the entity is GC'd.

    In your configuration, you can set enableEntityTracking=false, if you don't want the desired behavior.

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 09-05-2006 10:23 AM In reply to

    • Elliveny
    • Top 500 Contributor
    • Joined on 09-04-2006
    • Posts 13
    • Points 305

    Re: Confused by caching(?) behaviour

    It works! Marvellous... thank you for your help.

    If I could voice an opinion... I think the examples/guidelines should set enableEntityTracking=false by default as this kind of behaviour is different from any newbie's expectation. While on the netTiers learning curve the entity tracking falls outside the bounds of understanding and, I believe, is better being bought to a developer's attention when he/she needs it.

    One further question on this matter - if the code is using an in-memory entity why does it perform the SQL query at all? It seems especially strange that it performs the query and then ignores the result.

    Thanks again - I'm glad to be back on track Big Smile [:D]

    • Post Points: 35
  • 11-06-2006 12:27 PM In reply to

    • gmaland
    • Not Ranked
    • Joined on 10-10-2006
    • Posts 1
    • Points 35

    Re: Confused by caching(?) behaviour

    I'd like to chime in here as another .Net Tiers newbie.  I don't know where to look for documentation on things like EntityTracking, besides scouring through these forums.  Is there an official helpfile or other document that describes these things?  I would especially value a document that describes each method and property of each object. And code examples of typical usage would also be extremely helpful. If such documents already exist, can someone tell me where they are?

    I am aware of the main documentation for .Net Tiers (eg:  http://wiki.nettiers.com/entitylayer), which is a great "1000 foot view", but just not very detailed.

    • Post Points: 35
  • 11-07-2006 3:48 PM In reply to

    • mwerner
    • Top 50 Contributor
    • Joined on 03-02-2006
    • Sweden
    • Posts 106
    • Points 2,376

    Re: Confused by caching(?) behaviour

    How do you newbies feel about this http://community.codesmithtools.com/forums/thread/19115.aspx ? I'd like to hear your opinion. Is it understandable? Would that be a workable solution for you?
    Best regards, Magnus Werner
    • Post Points: 65
  • 11-10-2006 10:09 AM In reply to

    Re: Confused by caching(?) behaviour

    Hi Magnus,

    I like this solution, is there a patch?

    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 11-13-2006 4:40 AM In reply to

    • Tanno
    • Top 100 Contributor
    • Joined on 10-06-2006
    • Posts 50
    • Points 1,215

    Re: Confused by caching(?) behaviour

    Hi Werner,

    After you studied in detail the entity caching behaviour, do you have any idea how one could apply the entity caching for a delayed insert of an entity in a wizard ?

    In other words: save the input to an entity, cache the entity (without saving it to database) and at the last page of  the wizard read the entity from cache again to save it in an explicit transaction ?

    I made a post about this with more details (the post about the wizard insert) but still didn't get a usefull answer.

    thanks in advance

    Tanno

     

      

    Filed under: ,
    • Post Points: 35
  • 12-05-2006 2:06 PM In reply to

    • mwerner
    • Top 50 Contributor
    • Joined on 03-02-2006
    • Sweden
    • Posts 106
    • Points 2,376

    Re: Confused by caching(?) behaviour

    Sorry I've been away. I have a patch but I'm not quite happy about it since you really need to change the way tracking is handled when changes are involved. I'll put some effort into it and send you the stuff.
    Best regards, Magnus Werner
    • Post Points: 5
  • 12-05-2006 2:10 PM In reply to

    • mwerner
    • Top 50 Contributor
    • Joined on 03-02-2006
    • Sweden
    • Posts 106
    • Points 2,376

    Re: Confused by caching(?) behaviour

    Hi Tanno!

    Yes I've thought about it. The way I'd like to handle it is to allow for changed entities to be tracked as well. I've just haven't tried this out in reallity yet. I also have to think through how to implement this in order to be backwards compatible with code already deployed. I' ll have to add another property on the Provider to allow users to controll this. I'll do some work on this during the week end hoping to give you something usefull next week. Stay tuned.

    Best regards, Magnus Werner
    • Post Points: 5
  • 12-11-2006 8:04 AM In reply to

    • mwerner
    • Top 50 Contributor
    • Joined on 03-02-2006
    • Sweden
    • Posts 106
    • Points 2,376

    Re: Confused by caching(?) behaviour

    Hi Tanno!

    Just posted a contribution that changes the way entities are tracked and loaded http://community.codesmithtools.com/forums/thread/20634.aspx. I didn't get the time to do the insert case, also updates could be handled better i.e. entities would be tracked across a Save. The current post handles the case where you changes an existing entity. That entity you would be able to get from the EntityManager using the tracking key (based on the primary key values at the time of load). I'll put the improvement you want on the to-do list but as I'm doing this in my spare time I can't make any promise as to when it might be implemented. Also I guess it depends on whether the current post will make it into the official code base or not.

    Best regards, Magnus Werner
    • Post Points: 35
  • 12-11-2006 6:42 PM In reply to

    Re: Confused by caching(?) behaviour

    Magnus, thanks for posting this. I've been watching this thread since I recently discovered this same issue in my project.  I was a surprised at the unexpected behaviour of cached data which led me to find out the default use of the enableEntityTracking key and then ultimately to this thread. I have a feeling that many others will encounter this behaviour. Hopefully, this change will make it into the official nettiers implementation.

    • Post Points: 5
Page 1 of 1 (14 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems