static methods and concurrency?

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

    #1

    static methods and concurrency?

    Are there any issues with concurrency on static methods creating instances
    such as in the data access code below? Or if I wanted to expand it to use an
    ADO.NET transaction and having two users access this method at the same time?

    As I understand it, it would not be an issue because the variables being
    referenced aren't static fields and are either created inside the method or
    passed in.

    In other words is there any real difference between this code executing
    directly in a aspx codebehind vs in a static method of a class?.

    public class Orders
    {
    public static void UpdateOrder(str ing value1, string value2, etc)
    {
    SqlConnection cn = new SqlConnection(" connectionstrin g");
    SqlCommand cmd = new SqlCommand("Upd ateOrderStoredP roc", cn);
    ...
    cmd.ExecuteNonQ uery();
    }
    }
  • Mark R. Dawson

    #2
    RE: static methods and concurrency?

    Hi Dave,
    you are correct, static variables can only be instantiated once per app
    domain, but assigned many times. For example this does NOT compile because
    this code can be executed multiple times:

    static void DoSomething()
    {
    static int x = 5;
    }

    The variables inside the static function are local and are therefore
    created for each thread of control that passes through them each time the
    function is called, so there is no way two threads can interact with these
    variables at the same time.

    If you want to have static variables that are thread safe then you can use
    the ThreadStaticAtt ribute which will allow one instance of the variable per
    thread.


    Mark Dawson
    --



    "Dave" wrote:
    [color=blue]
    > Are there any issues with concurrency on static methods creating instances
    > such as in the data access code below? Or if I wanted to expand it to use an
    > ADO.NET transaction and having two users access this method at the same time?
    >
    > As I understand it, it would not be an issue because the variables being
    > referenced aren't static fields and are either created inside the method or
    > passed in.
    >
    > In other words is there any real difference between this code executing
    > directly in a aspx codebehind vs in a static method of a class?.
    >
    > public class Orders
    > {
    > public static void UpdateOrder(str ing value1, string value2, etc)
    > {
    > SqlConnection cn = new SqlConnection(" connectionstrin g");
    > SqlCommand cmd = new SqlCommand("Upd ateOrderStoredP roc", cn);
    > ...
    > cmd.ExecuteNonQ uery();
    > }
    > }[/color]

    Comment

    • William Stacey [MVP]

      #3
      Re: static methods and concurrency?

      Its a static method, but your vars are instance vars, so it looks safe to me
      as long as your not using any other shared vars in the method.

      --
      William Stacey [MVP]

      "Dave" <Dave@discussio ns.microsoft.co m> wrote in message
      news:40A6A821-636F-4E19-8FC2-526D17036A1F@mi crosoft.com...
      | Are there any issues with concurrency on static methods creating instances
      | such as in the data access code below? Or if I wanted to expand it to use
      an
      | ADO.NET transaction and having two users access this method at the same
      time?
      |
      | As I understand it, it would not be an issue because the variables being
      | referenced aren't static fields and are either created inside the method
      or
      | passed in.
      |
      | In other words is there any real difference between this code executing
      | directly in a aspx codebehind vs in a static method of a class?.
      |
      | public class Orders
      | {
      | public static void UpdateOrder(str ing value1, string value2, etc)
      | {
      | SqlConnection cn = new SqlConnection(" connectionstrin g");
      | SqlCommand cmd = new SqlCommand("Upd ateOrderStoredP roc", cn);
      | ...
      | cmd.ExecuteNonQ uery();
      | }
      | }


      Comment

      • Peter Bromberg [C# MVP]

        #4
        RE: static methods and concurrency?

        Dave,
        All of the methods in the widely used DAAB v2 "SqlHelper" class are set up
        exactly like this, as public static methods.
        Peter
        --
        Co-founder, Eggheadcafe.com developer portal:

        UnBlog:





        "Dave" wrote:
        [color=blue]
        > Are there any issues with concurrency on static methods creating instances
        > such as in the data access code below? Or if I wanted to expand it to use an
        > ADO.NET transaction and having two users access this method at the same time?
        >
        > As I understand it, it would not be an issue because the variables being
        > referenced aren't static fields and are either created inside the method or
        > passed in.
        >
        > In other words is there any real difference between this code executing
        > directly in a aspx codebehind vs in a static method of a class?.
        >
        > public class Orders
        > {
        > public static void UpdateOrder(str ing value1, string value2, etc)
        > {
        > SqlConnection cn = new SqlConnection(" connectionstrin g");
        > SqlCommand cmd = new SqlCommand("Upd ateOrderStoredP roc", cn);
        > ...
        > cmd.ExecuteNonQ uery();
        > }
        > }[/color]

        Comment

        Working...