473,748 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 NullReferenceEx ception 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.GetN onCachedData();
DataSet blah = SomeObject.GetC achedDataSet();
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 1448
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.co m

"Schroeder" <kw******@palom a.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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 NullReferenceEx ception 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.GetN onCachedData();
DataSet blah = SomeObject.GetC achedDataSet();
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******@palom a.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 NullReferenceEx ception 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.GetN onCachedData();
DataSet blah = SomeObject.GetC achedDataSet();
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.co m>
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.co m

"Schroeder" <kw******@palom a.com> wrote in message
news:11******** *************@z 14g2000cwz.goog legroups.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.c om> 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.co m>
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 nonSharedDataSe t = SomeObject.GetN onCachedData(); // not
shared data...unique to this method
DataSet sharedDataSet = SomeObject.GetC achedDataSet(); // shared
data
lock(sharedData Set) {
nonSharedDataSe t.Merge(sharedD ataSet);
}

return nonSharedDataSe t
}

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******@palom a.com> wrote:
I "refactored ' the code to make it more clear.

public DataSet SomeMethod() {
DataSet nonSharedDataSe t = SomeObject.GetN onCachedData(); // not
shared data...unique to this method
DataSet sharedDataSet = SomeObject.GetC achedDataSet(); // shared
data
lock(sharedData Set) {
nonSharedDataSe t.Merge(sharedD ataSet);
}

return nonSharedDataSe t
}

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.co m>
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
4044
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 I am not fully convinced that this problem cannot be solved. I am going to propose a solution here. For the sake of discussion I will post my solution here. It is possible that the proposed solution does not work, feedback and comments are...
4
4560
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 has almost all of it public methods Java synchronized: > > http://cvs.sourceforge.net/viewcvs.py/jython/jython/org/python/core/PyStringMap.
2
1350
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 slow). it provides a cached copy of this table to clients, for them to read from (note: there are no writes). the idea being if a particular table retrival takes time, the class would still provide a cached copy. now the code:
0
1520
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 referenced. The type of the data must be a Python dict (comes from an external source). Here's what I've got so far: import time
0
2094
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 to the same table. During the simultaneous add and update, a Locked error occurs, and most of the times it locks all the machines. I have been reading on Microsoft Site and on google groups that JET4 and ADO have fix for page locking , which...
10
11913
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 reference to (this). In the case of these static methods I am not able to do that.
14
1363
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) { use shared_object; } pattern. I do not see any reason to have another simple object to lock for since I can lock the shared object directly. Are there any deep implications to use shared lock object to guard the actual shared object, maybe...
6
1877
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, SomeOtherClass>(); object lockObject = new object(); --- Example 1 ---
0
153
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
9357
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9309
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8235
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6072
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4591
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3298
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2205
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.