473,378 Members | 1,447 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,378 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 1440
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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?
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...

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.