Locking Static DB Load Method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?cmFuZHkxMjAw?=

    #1

    Locking Static DB Load Method

    I'm working in Visual Studio 2005.

    I have the following method. I'm trying to figure out why I wouldn't want to
    wrap the contents in a lock(MyLockObje ct) {...} Seems to me that having a
    static method in a mutlithreaded environment isn't thread safe.

    public static MyDataSet Load()
    {
    //Open database connection
    //Load data from database into dataset
    //Close connection
    }

    Any thoughts?

    Thanks,
    Randy
  • Jon Skeet [C# MVP]

    #2
    Re: Locking Static DB Load Method

    randy1200 <randy1200@disc ussions.microso ft.comwrote:
    I'm working in Visual Studio 2005.
    >
    I have the following method. I'm trying to figure out why I wouldn't want to
    wrap the contents in a lock(MyLockObje ct) {...} Seems to me that having a
    static method in a mutlithreaded environment isn't thread safe.
    >
    public static MyDataSet Load()
    {
    //Open database connection
    //Load data from database into dataset
    //Close connection
    }
    >
    Any thoughts?
    Well, is that method going to use any shared data? If not, what's the
    problem with multiple threads executing it at a time?

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Locking Static DB Load Method

      randy,

      I don't see a particular need based on the example you provided. When
      working with the database, the provider to the data source is more than
      likely going to handle concurrency issues to the underlying data source.
      Beyond that, unless you are accessing something in the static method which
      is shared state, I wouldn't worry about locking anything.

      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "randy1200" <randy1200@disc ussions.microso ft.comwrote in message
      news:4CC0171C-601A-412D-AC8D-7E4CD7617314@mi crosoft.com...
      I'm working in Visual Studio 2005.
      >
      I have the following method. I'm trying to figure out why I wouldn't want
      to
      wrap the contents in a lock(MyLockObje ct) {...} Seems to me that having a
      static method in a mutlithreaded environment isn't thread safe.
      >
      public static MyDataSet Load()
      {
      //Open database connection
      //Load data from database into dataset
      //Close connection
      }
      >
      Any thoughts?
      >
      Thanks,
      Randy

      Comment

      • =?Utf-8?B?cmFuZHkxMjAw?=

        #4
        Re: Locking Static DB Load Method

        Thanks for the response.

        This static method is set up to get a single record from a database.
        Initially, calls to this method were made serially, so I was not concerned at
        all about threading. Now, calls are made concurrently from multiple threads,
        so I'm concerned about a subsequent call trashing data in a previous call and
        getting data corruption.

        I guess I need to put a finer point on what's meant by "shared data." Even
        though each call only returns a single record, I'm still returning each
        record as a DataTable (not just a row), and the DataTables get merged upon
        return.

        Any more thoughts are greatly appreciated.

        Randy


        "Jon Skeet [C# MVP]" wrote:
        randy1200 <randy1200@disc ussions.microso ft.comwrote:
        I'm working in Visual Studio 2005.

        I have the following method. I'm trying to figure out why I wouldn't want to
        wrap the contents in a lock(MyLockObje ct) {...} Seems to me that having a
        static method in a mutlithreaded environment isn't thread safe.

        public static MyDataSet Load()
        {
        //Open database connection
        //Load data from database into dataset
        //Close connection
        }

        Any thoughts?
        >
        Well, is that method going to use any shared data? If not, what's the
        problem with multiple threads executing it at a time?
        >
        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too
        >

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Locking Static DB Load Method

          randy1200 <randy1200@disc ussions.microso ft.comwrote:
          Thanks for the response.
          >
          This static method is set up to get a single record from a database.
          Initially, calls to this method were made serially, so I was not concerned at
          all about threading. Now, calls are made concurrently from multiple threads,
          so I'm concerned about a subsequent call trashing data in a previous call and
          getting data corruption.
          How would it do that?
          I guess I need to put a finer point on what's meant by "shared data." Even
          though each call only returns a single record, I'm still returning each
          record as a DataTable (not just a row), and the DataTables get merged upon
          return.
          Where though? In whatever method is calling Load()? If so, it's up to
          that method to sort things out.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          Working...