Static methods and local data (Again)

S

Simon Harvey

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
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Simon,

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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,
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top