473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Locking Reference Objects

I have a DataSet that is cached deep down in a business layer object.
Higher up, there's a merge being performed on that object and it very
occassionaly throws a NullReferenceException deep down in the merge
code. I suspect that the DataSet is being edited during the merge by
another thread or its going out of scope (i.e. expiring).

The question is whether I can/should lock that reference object. Would
it help?

For example:

public SomeMethod() {
DataSet moreData = SomeObject.GetNonCachedData();
DataSet blah = SomeObject.GetCachedDataSet();
lock(blah) {
moreData.Merge(blah);
}
}

In this case 'blah' is a local object so it's not a threading issue per
se. I just think that another thread is affecting the cache and since
the DataSet is a reference object, it's causing this method to blow up.
What I'm really looking for is the best way to keep that reference
object from being edited during the merge. Is using that lock going to
help me?

Dec 21 '05 #1
7 1433
Schroeder,

Is moreData a reference that is returned that is shared? If so, then
you should probably lock on this before you make changes to it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Schroeder" <kw******@paloma.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I have a DataSet that is cached deep down in a business layer object.
Higher up, there's a merge being performed on that object and it very
occassionaly throws a NullReferenceException deep down in the merge
code. I suspect that the DataSet is being edited during the merge by
another thread or its going out of scope (i.e. expiring).

The question is whether I can/should lock that reference object. Would
it help?

For example:

public SomeMethod() {
DataSet moreData = SomeObject.GetNonCachedData();
DataSet blah = SomeObject.GetCachedDataSet();
lock(blah) {
moreData.Merge(blah);
}
}

In this case 'blah' is a local object so it's not a threading issue per
se. I just think that another thread is affecting the cache and since
the DataSet is a reference object, it's causing this method to blow up.
What I'm really looking for is the best way to keep that reference
object from being edited during the merge. Is using that lock going to
help me?

Dec 21 '05 #2
Schroeder <kw******@paloma.com> wrote:
I have a DataSet that is cached deep down in a business layer object.
Higher up, there's a merge being performed on that object and it very
occassionaly throws a NullReferenceException deep down in the merge
code. I suspect that the DataSet is being edited during the merge by
another thread or its going out of scope (i.e. expiring).

The question is whether I can/should lock that reference object. Would
it help?
Very unlikely. One thread locking on something doesn't stop another
thread from doing something unless the second thread *also* tries to
lock on the same thing.
For example:

public SomeMethod() {
DataSet moreData = SomeObject.GetNonCachedData();
DataSet blah = SomeObject.GetCachedDataSet();
lock(blah) {
moreData.Merge(blah);
}
}

In this case 'blah' is a local object so it's not a threading issue per
se. I just think that another thread is affecting the cache and since
the DataSet is a reference object, it's causing this method to blow up.
What I'm really looking for is the best way to keep that reference
object from being edited during the merge. Is using that lock going to
help me?


Is this DataSet bound to any UI controls? If so, you shouldn't let any
thread other than the UI thread update it. Might that be part of the
problem?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #3
moreData is not a reference to any shared data. It is only used by the
method in question.

Dec 21 '05 #4
Well, which ones are shared then?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Schroeder" <kw******@paloma.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
moreData is not a reference to any shared data. It is only used by the
method in question.

Dec 21 '05 #5
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Is moreData a reference that is returned that is shared? If so, then
you should probably lock on this before you make changes to it.


That won't do any good unless anything else that uses it *also* locks
on it first.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #6
I "refactored' the code to make it more clear.

public DataSet SomeMethod() {
DataSet nonSharedDataSet = SomeObject.GetNonCachedData(); // not
shared data...unique to this method
DataSet sharedDataSet = SomeObject.GetCachedDataSet(); // shared
data
lock(sharedDataSet) {
nonSharedDataSet.Merge(sharedDataSet);
}

return nonSharedDataSet
}

To answer Jon's question... yes, the results are being bound to a
control. The problem is that since it's working off of a shared cache
I can't really prevent other threads from modifying it. Or, at the
very least, I can't be sure that the cache isn't going to get blown
away by the cache engine due to age or memory usage.

Dec 21 '05 #7
Schroeder <kw******@paloma.com> wrote:
I "refactored' the code to make it more clear.

public DataSet SomeMethod() {
DataSet nonSharedDataSet = SomeObject.GetNonCachedData(); // not
shared data...unique to this method
DataSet sharedDataSet = SomeObject.GetCachedDataSet(); // shared
data
lock(sharedDataSet) {
nonSharedDataSet.Merge(sharedDataSet);
}

return nonSharedDataSet
}

To answer Jon's question... yes, the results are being bound to a
control. The problem is that since it's working off of a shared cache
I can't really prevent other threads from modifying it. Or, at the
very least, I can't be sure that the cache isn't going to get blown
away by the cache engine due to age or memory usage.


The cache being blown away isn't a problem, but you *must not* modify a
DataSet when it's bound to controls, unless you're "in" the UI thread.
The events fired would mean UI code getting executed in the wrong
thread.

This may or may not be what's causing your problem, but it's a
potentially serious design issue for you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
4
by: Michael Chermside | last post by:
Ype writes: > For the namespaces in Jython this 'Python internal thread safety' > is handled by the Java class: > > http://www.jython.org/docs/javadoc/org/python/core/PyStringMap.html > > which...
2
by: spammy | last post by:
hi all, im trying to establish whether i have a race condition or critical section in the following. i have a dataaccess class that continually retireves a table from a sqlserver (which may be...
0
by: Timo | last post by:
I'm trying to make a thread safe object cache without locking. The objects are cached by the id of the data dict given in __new__. Objects are removed from the cache as soon as they are no longer...
0
by: brijeshmathew | last post by:
Hi I use Visual Basic 6, Service Pack 6, Microsoft ActiveX Data Objects 2.8 Library(msado15.dll) and access 2000 database using JET 4 OLE. I have an application that adds records simultaneously...
10
by: McFly Racing | last post by:
Thread Locking In Static Methods I have the need for a Log Manger class that has static methods. Normally I would use the lock statement or a Monitor statement both of which take a...
14
by: Laura T. | last post by:
To synchronize thread access to shared objects, I've always used the lock(shared_object) { } pattern. I've seen many recommendations and examples where to synchronize use lock(another_object) {...
6
by: Akula | last post by:
Does anyone know whether or not it is faster to lock a simple object, rather than a complex type? For example: Dictionary<string, SomeOtherClassdict = new Dictionary<string,...
0
by: =?UTF-8?B?TmlscyBPbGl2ZXIgS3LDtmdlcg==?= | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I don't think the global interpreter lock is what you need ... read here for reference: http://docs.python.org/api/threads.html
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.