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

Static methods and local data (Again)

Hi everyone,

I'm still abit confused about static methods accessing locally declared
variables.

For example

public static bool executeNonQuery(){
SqlCommand cmd;
SqlConnection con;
.....
}
Is the above threadsafe? Can the cmd or con object be accessed by more than
one thread at the same time.

What if the code was:
public static bool executeNonQuery(SqlCommand cmd){
SqlConnection con;
cmd.Connection = con;
.....
}
Where the command object is also created in a static calling method?

Many thanks all

Simon
Jul 21 '05 #1
2 1445
Hi Simon,

<snip>
For example

public static bool executeNonQuery(){
SqlCommand cmd;
SqlConnection con;
....
} Is the above threadsafe? Can the cmd or con object be accessed by more than
one thread at the same time.
Only one thread can use local variables initialized in this method.
Doesn't matter if it is "static" or not.

First danger in multi-threading is when one thread changes state
of "class variables" or "static" variables.
Second danger is that you can not be sure which thread
returns from the method as a first one.
What if the code was:
public static bool executeNonQuery(SqlCommand cmd){
SqlConnection con;
cmd.Connection = con;
....
}

Where the command object is also created in a static calling method?


If you initialize connection in this method then only thing
that you should affraid is data in database.

Regards

Marcin
Jul 21 '05 #2
Hi Simon,

public static bool executeNonQuery(){
SqlCommand cmd;
SqlConnection con;
....
}

Each time that you call that method two spaces to hold a reference are
created in the stack, it's the same thing if you call this from several
thread or if executeNonQuery() is a recursive method, no matter what you
need to do a
cmd = new SqlCommand( ... )
inside the method, this will create a new instance each time you call it.

therefore each thread or call to the method will have its OWN created
objects. therefore you have no problem as everybody is using a different
instance.
Now a different escenario is if you use a object that is created outside the
method. in this case if more than one call is done this object is shared
among all the invokes, and you need to deal with it.


public static bool executeNonQuery(SqlCommand cmd){
SqlConnection con;
cmd.Connection = con;
....
}
Where the command object is also created in a static calling method?

if the method is ALWAYS called from that method and the cmd object is
created inside that method, then you have the same escenario that above.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jul 21 '05 #3

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

Similar topics

5
by: GIMME | last post by:
One of my coworkers insists that one should never use static methods because race conditions exist. Thinking along the lines that all variable assignments are assignments to static variables. ...
1
by: baylor | last post by:
In C#, an interface cannot mark any method as static. i'm told the ILASM supports it but i've never tested that Two questions. First, why? OK, i've heard the reason about interfaces being...
2
by: Simon Harvey | last post by:
Hi everyone, I'm still abit confused about static methods accessing locally declared variables. For example public static bool executeNonQuery(){ SqlCommand cmd; SqlConnection con;
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...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
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
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
15
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.