Simultaneous modifying a disconnected dataset in Cache?

Last post 01-24-2006 8:43 AM by mbanavige. 3 replies.

Sort Posts:

  • Simultaneous modifying a disconnected dataset in Cache?

    01-23-2006, 4:25 PM
    • Contributor
      3,231 point Contributor
    • sun21170
    • Member since 03-15-2003, 8:56 AM
    • Dallas,USA
    • Posts 914

    Should I be concerned, if in my code I am at times modifying the dataset stored in application cache? I am thinking what would happen if two requests over the web cause the dataset in cache to be modified at the same time. OR ASP.Net will automatically lock the cache data during modification.

    By modifying the dataset, I mean changing the dataset row data in 'memory', without going to the database. For example, I may change the 'ParentID' column of a specific datarow to the value '1125'.

    I am using the 'Cache' object of the Page class to store data? The Application.State object provides a lock method when modifying the state data, but 'Cache' object does not have any such method.

    Thanks

    sun21170
  • Re: Simultaneous modifying a disconnected dataset in Cache?

    01-23-2006, 4:50 PM
    • All-Star
      97,048 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,261
    • Moderator
      TrustedFriends-MVPs

    The Cache actually holds references the real object that exists in memory - thus keeping that object alive.

    When you retrieve the object from the cache, you are actually retrieving a reference to a live object that another person on another page  may also be referencing.  If you do not implement any form of locking during updates to that objects properties, then it is entirely possible that multiple users could make incompatible changes.

    Here's an interesting read: http://west-wind.com/weblog/posts/1214.aspx

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Simultaneous modifying a disconnected dataset in Cache?

    01-24-2006, 2:14 AM
    • Contributor
      3,231 point Contributor
    • sun21170
    • Member since 03-15-2003, 8:56 AM
    • Dallas,USA
    • Posts 914
    But a cached item does have an inbuilt locking mechanism since it's thread safe (the official documentation says about Cache type - "This type is safe for multithreaded operations."). So no 2 users (2 users means 2 different threads in ASP.Net) can modify a cached item at the same time. Then it seems no locking needs to be implemented for a cached item.
    sun21170
  • Re: Simultaneous modifying a disconnected dataset in Cache?

    01-24-2006, 8:43 AM
    • All-Star
      97,048 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,261
    • Moderator
      TrustedFriends-MVPs

    The thread safety you refer to applies to reading and/or writing to single items of the Cache.
    This thread safety does not apply to the objects themsleves once you have obtained a reference to them from the Cache.

    If you begin manipulating properties of a dataset that was retrieved from the Cache, it is absolutely possible for two threads to make inconsistent updates unless you perform some form of synchronization for your dataset object.

     

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
Page 1 of 1 (4 items)