Locking Static DB Load Method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
randy1200 said:
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?
 
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.
 
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
 
randy1200 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top