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.
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.
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.
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.
sun21170
Contributor
3419 Points
1177 Posts
Simultaneous modifying a disconnected dataset in Cache?
Jan 23, 2006 08:25 PM|LINK
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
mbanavige
All-Star
130719 Points
14322 Posts
ASPInsiders
Moderator
MVP
Re: Simultaneous modifying a disconnected dataset in Cache?
Jan 23, 2006 08:50 PM|LINK
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
sun21170
Contributor
3419 Points
1177 Posts
Re: Simultaneous modifying a disconnected dataset in Cache?
Jan 24, 2006 06:14 AM|LINK
mbanavige
All-Star
130719 Points
14322 Posts
ASPInsiders
Moderator
MVP
Re: Simultaneous modifying a disconnected dataset in Cache?
Jan 24, 2006 12:43 PM|LINK
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.