Static methods and local data (Again)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Harvey

    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


  • Marcin Grzêbski

    #2
    Re: Static methods and local data (Again)

    Hi Simon,

    <snip>
    [color=blue]
    > For example
    >
    > public static bool executeNonQuery (){
    > SqlCommand cmd;
    > SqlConnection con;
    > ....
    > }[/color]
    [color=blue]
    > Is the above threadsafe? Can the cmd or con object be accessed by more than
    > one thread at the same time.[/color]

    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.
    [color=blue]
    > 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?[/color]

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

    Regards

    Marcin

    Comment

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

      #3
      Re: Static methods and local data (Again)

      Hi Simon,

      [color=blue]
      > public static bool executeNonQuery (){
      > SqlCommand cmd;
      > SqlConnection con;
      > ....
      > }[/color]


      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.

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


      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


      Comment

      Working...