Static methods and local data (Again)

Discussion in 'ASP .Net' started by Simon Harvey, Jul 2, 2004.

  1. Simon Harvey

    Simon Harvey Guest

    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
    Simon Harvey, Jul 2, 2004
    #1
    1. Advertising

  2. 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
    =?ISO-8859-2?Q?Marcin_Grz=EAbski?=, Jul 2, 2004
    #2
    1. Advertising

  3. 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
    Ignacio Machin \( .NET/ C# MVP \), Jul 2, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,386
    Tony Morris
    Oct 19, 2004
  2. Patrick Hoffmann
    Replies:
    3
    Views:
    2,828
    Christian Jan├čen
    Aug 8, 2003
  3. o_ssie
    Replies:
    0
    Views:
    315
    o_ssie
    Apr 9, 2009
  4. Tomas Mikula

    abstract static methods (again)

    Tomas Mikula, Oct 19, 2009, in forum: Java
    Replies:
    67
    Views:
    1,988
    Tomas Mikula
    Nov 4, 2009
  5. Leslaw Bieniasz

    local static data vs. global data

    Leslaw Bieniasz, Oct 23, 2012, in forum: C++
    Replies:
    3
    Views:
    387
    Werner
    Oct 24, 2012
Loading...

Share This Page