473,385 Members | 1,908 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,385 software developers and data experts.

Locking Static DB Load Method

I'm working in Visual Studio 2005.

I have the following method. I'm trying to figure out why I wouldn't want to
wrap the contents in a lock(MyLockObject) {...} Seems to me that having a
static method in a mutlithreaded environment isn't thread safe.

public static MyDataSet Load()
{
//Open database connection
//Load data from database into dataset
//Close connection
}

Any thoughts?

Thanks,
Randy
Oct 18 '07 #1
4 1506
randy1200 <ra*******@discussions.microsoft.comwrote:
I'm working in Visual Studio 2005.

I have the following method. I'm trying to figure out why I wouldn't want to
wrap the contents in a lock(MyLockObject) {...} Seems to me that having a
static method in a mutlithreaded environment isn't thread safe.

public static MyDataSet Load()
{
//Open database connection
//Load data from database into dataset
//Close connection
}

Any thoughts?
Well, is that method going to use any shared data? If not, what's the
problem with multiple threads executing it at a time?

--
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
Oct 18 '07 #2
randy,

I don't see a particular need based on the example you provided. When
working with the database, the provider to the data source is more than
likely going to handle concurrency issues to the underlying data source.
Beyond that, unless you are accessing something in the static method which
is shared state, I wouldn't worry about locking anything.

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

"randy1200" <ra*******@discussions.microsoft.comwrote in message
news:4C**********************************@microsof t.com...
I'm working in Visual Studio 2005.

I have the following method. I'm trying to figure out why I wouldn't want
to
wrap the contents in a lock(MyLockObject) {...} Seems to me that having a
static method in a mutlithreaded environment isn't thread safe.

public static MyDataSet Load()
{
//Open database connection
//Load data from database into dataset
//Close connection
}

Any thoughts?

Thanks,
Randy

Oct 18 '07 #3
Thanks for the response.

This static method is set up to get a single record from a database.
Initially, calls to this method were made serially, so I was not concerned at
all about threading. Now, calls are made concurrently from multiple threads,
so I'm concerned about a subsequent call trashing data in a previous call and
getting data corruption.

I guess I need to put a finer point on what's meant by "shared data." Even
though each call only returns a single record, I'm still returning each
record as a DataTable (not just a row), and the DataTables get merged upon
return.

Any more thoughts are greatly appreciated.

Randy
"Jon Skeet [C# MVP]" wrote:
randy1200 <ra*******@discussions.microsoft.comwrote:
I'm working in Visual Studio 2005.

I have the following method. I'm trying to figure out why I wouldn't want to
wrap the contents in a lock(MyLockObject) {...} Seems to me that having a
static method in a mutlithreaded environment isn't thread safe.

public static MyDataSet Load()
{
//Open database connection
//Load data from database into dataset
//Close connection
}

Any thoughts?

Well, is that method going to use any shared data? If not, what's the
problem with multiple threads executing it at a time?

--
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
Oct 18 '07 #4
randy1200 <ra*******@discussions.microsoft.comwrote:
Thanks for the response.

This static method is set up to get a single record from a database.
Initially, calls to this method were made serially, so I was not concerned at
all about threading. Now, calls are made concurrently from multiple threads,
so I'm concerned about a subsequent call trashing data in a previous call and
getting data corruption.
How would it do that?
I guess I need to put a finer point on what's meant by "shared data." Even
though each call only returns a single record, I'm still returning each
record as a DataTable (not just a row), and the DataTables get merged upon
return.
Where though? In whatever method is calling Load()? If so, it's up to
that method to sort things out.

--
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
Oct 18 '07 #5

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...
5
by: Chris | last post by:
Hi I have a scenario where I've created another AppDomain to dynamically load a DLL(s) into. In this newly loaded DLL I want to call a static method on a class. The problem arise is that I have...
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...
6
by: Mirek Endys | last post by:
Hello all, another problem im solving right now. I badly need to get typeof object that called static method in base classe. I did it by parameter in method Load, but i thing there should be...
3
by: crgsmrt | last post by:
Hi all, I'd really appreciate it if someone was able to help me out with a problem I'm having with C#. I'm a complete newbie to this, so please excuse what may appear as a stupid question. ...
7
by: Shak | last post by:
Hi all, I'm trying to write a thread-safe async method to send a message of the form (type)(contents). My model is as follows: private void SendMessage(int type, string message) { //lets...
1
by: Mark S. | last post by:
Hello, My c# aspx 2.0 web application has a static class. I would like to add a dataTable to it and have the heavy load (+100 requests a second) update it's values. This is the first time I used...
0
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of...
14
by: Jordan Marr | last post by:
I have the following class: class ProvisionCollection { ... private int m_VarianceCount; public int VarianceCount { get { return m_VarianceCount; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.