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

static methods and concurrency?

Are there any issues with concurrency on static methods creating instances
such as in the data access code below? Or if I wanted to expand it to use an
ADO.NET transaction and having two users access this method at the same time?

As I understand it, it would not be an issue because the variables being
referenced aren't static fields and are either created inside the method or
passed in.

In other words is there any real difference between this code executing
directly in a aspx codebehind vs in a static method of a class?.

public class Orders
{
public static void UpdateOrder(string value1, string value2, etc)
{
SqlConnection cn = new SqlConnection("connectionstring");
SqlCommand cmd = new SqlCommand("UpdateOrderStoredProc", cn);
...
cmd.ExecuteNonQuery();
}
}
Feb 24 '06 #1
3 7304
Hi Dave,
you are correct, static variables can only be instantiated once per app
domain, but assigned many times. For example this does NOT compile because
this code can be executed multiple times:

static void DoSomething()
{
static int x = 5;
}

The variables inside the static function are local and are therefore
created for each thread of control that passes through them each time the
function is called, so there is no way two threads can interact with these
variables at the same time.

If you want to have static variables that are thread safe then you can use
the ThreadStaticAttribute which will allow one instance of the variable per
thread.
Mark Dawson
--
http://www.markdawson.org
"Dave" wrote:
Are there any issues with concurrency on static methods creating instances
such as in the data access code below? Or if I wanted to expand it to use an
ADO.NET transaction and having two users access this method at the same time?

As I understand it, it would not be an issue because the variables being
referenced aren't static fields and are either created inside the method or
passed in.

In other words is there any real difference between this code executing
directly in a aspx codebehind vs in a static method of a class?.

public class Orders
{
public static void UpdateOrder(string value1, string value2, etc)
{
SqlConnection cn = new SqlConnection("connectionstring");
SqlCommand cmd = new SqlCommand("UpdateOrderStoredProc", cn);
...
cmd.ExecuteNonQuery();
}
}

Feb 24 '06 #2
Its a static method, but your vars are instance vars, so it looks safe to me
as long as your not using any other shared vars in the method.

--
William Stacey [MVP]

"Dave" <Da**@discussions.microsoft.com> wrote in message
news:40**********************************@microsof t.com...
| Are there any issues with concurrency on static methods creating instances
| such as in the data access code below? Or if I wanted to expand it to use
an
| ADO.NET transaction and having two users access this method at the same
time?
|
| As I understand it, it would not be an issue because the variables being
| referenced aren't static fields and are either created inside the method
or
| passed in.
|
| In other words is there any real difference between this code executing
| directly in a aspx codebehind vs in a static method of a class?.
|
| public class Orders
| {
| public static void UpdateOrder(string value1, string value2, etc)
| {
| SqlConnection cn = new SqlConnection("connectionstring");
| SqlCommand cmd = new SqlCommand("UpdateOrderStoredProc", cn);
| ...
| cmd.ExecuteNonQuery();
| }
| }
Feb 24 '06 #3
Dave,
All of the methods in the widely used DAAB v2 "SqlHelper" class are set up
exactly like this, as public static methods.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Dave" wrote:
Are there any issues with concurrency on static methods creating instances
such as in the data access code below? Or if I wanted to expand it to use an
ADO.NET transaction and having two users access this method at the same time?

As I understand it, it would not be an issue because the variables being
referenced aren't static fields and are either created inside the method or
passed in.

In other words is there any real difference between this code executing
directly in a aspx codebehind vs in a static method of a class?.

public class Orders
{
public static void UpdateOrder(string value1, string value2, etc)
{
SqlConnection cn = new SqlConnection("connectionstring");
SqlCommand cmd = new SqlCommand("UpdateOrderStoredProc", cn);
...
cmd.ExecuteNonQuery();
}
}

Feb 25 '06 #4

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

Similar topics

13
by: Axehelm | last post by:
Okay, I'm in a debate over whether or not static methods are a good idea in a general domain class. I'm personally not a fan of static methods but we seem to be using them to load an object. ...
3
by: Steven D'Aprano | last post by:
I've been doing a lot of reading about static methods in Python, and I'm not exactly sure what they are useful for or why they were introduced. Here is a typical description of them, this one...
8
by: Steven Livingstone | last post by:
Anyone able to explain to me why you cannot define an interface that can then be implemented using static methods? I understand the C# CLS states this, but just interested in the reasons behind...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
5
by: blah, blah, blah | last post by:
I'm developing a .Net web application and created many helper classes often using static (shared in VB.Net) methods. Do I need to use the lock (SyncLock) statement in these methods to prevent...
6
by: Simon Harvey | last post by:
Hi all, In my project I have made a number of helper methods static. As I understand it, this will create the problem that multiple threads could access the static method at the same time and...
15
by: dn | last post by:
I'm starting an n-tier application with an ASP.NET 2.0 presentation layer, a business layer, a data access layer, and a SQL Server 2005 database, and I have a question. In the business and data...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
12
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
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: 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:
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.