0

Let's say I have a method like this:

public class MyDataAccess
{
    public static StudentDataTable GetStudentRecords(int schoolID)
    {
        var ta = new StudentTableAdapter();
        using (ta.Connection = new SqlConnection(GetCS()))
        {
            var result = ta.GetStudentRecords(schoolID);
            return result;
        }
    }
}

If this method is accessed 1,000 times at a single time, using a console app or any other means, would there be any chance of it returning records for one school's student to another school?

e.g.

var records1 = GetStudentRecords(1);
.....
var records1000 = GetStudentRecords(1000);

Is there any chance of the records1 variable getting records of schoolID = 1 mixed with records from records1000, even if they calls come from their own threads?

Also what is going to happen if I change this class to static as well?

My understanding at this point

1) Yes, I understand what a class is

2) Yes, I know static classes don't need required instances to be used

3) I know simple static code should be there, for example converting datatypes.

But what I am confused with is:

What happens when there is a single, static class and there are a million objects trying to access the same object in STACK

especially when it's a dataAccess class, or a class which accesses activeDirectory objects.

I had a chat in the programmers SE chat room. They didn't say it explicitly, but I got the impression to never use static classes for querying e.g. sql, active directory, etc.. And that static classes should be limited to simple static code only - no querying.

Russ
  • 4,091
  • 21
  • 32
Mathematics
  • 7,314
  • 25
  • 77
  • 152
  • 3
    Test it and find out... – TheLethalCoder Aug 25 '15 at 13:47
  • Not an answer to your question, but this doesn't look like a good candidate for static to me. You may want to read this: http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp?rq=1 – Paddy Aug 25 '15 at 13:50
  • 1
    No, it's fine. `what going to happen if I change this class to static as well` Nothing, within the scope of your question. – Jonesopolis Aug 25 '15 at 13:50
  • 2
    The answer would be different if the table-adapter or the connection would be a static field instead of a local variable. – Tim Schmelter Aug 25 '15 at 13:53
  • @Jonesopolis I want to know other scopes as well, I want to know everything about statics, I can't seem to find a strong reference – Mathematics Aug 25 '15 at 13:54

2 Answers2

5

Looking at the code you've given us, the code seems thread-safe. There is no (in-memory) data being shared between threads.

My only concern would be at the db layer - make sure you're using the correct isolation level to avoid dirty reads.

Also what going to happen if I change this class to static as well.

In regards to GetStudentRecords? Nothing.


Here's a classic example of thread-unsafe code.

public class MyDataAccess
{

    private static int _retrievalCount = 0;

    public static StudentDataTable GetStudentRecords(int schoolID)
    {
        _retrievalCount ++;

        // retrieve and return records;            
    }
}

The field _retrievalCount is shared across threads. And the operation ++ is not atomic. That is, it involves three steps

  1. retrieving the integered stored in _retrievalCount
  2. adding 1 to it
  3. and storing the result back in _retrievalCount.

If two threads do this simultaneously, and these 3 steps are interleaved, then this could happen instead (assume _retrievalCount starts at 0 and there are 2 threads running):

  1. Thread A reads _retrievalCount as 0
  2. Thread A adds 1 to 0, result = 1
  3. Thread B reads _retrievalCount as 0
  4. Thread B adds 1 to 0, result = 1
  5. Thread A stores result=1 in _retrievalCount
  6. Thread B stores result=1 in _retrievalCount

The expected output was 2, but the actual output was 1.

dcastro
  • 66,540
  • 21
  • 145
  • 155
  • could you show me an example of how data gets shared between threads, maybe that is what I get confused it with – Mathematics Aug 25 '15 at 13:53
  • What is EXACTLY the thing that confuses you? You posted an apparently perfectly fine example of code, which really doesn't highlight nothing about how static works or is used for. If you specify, I can try to elaborate with an answer. – Alberto Chiesa Aug 25 '15 at 13:55
  • @Mathematics I've added an example. – dcastro Aug 25 '15 at 13:58
5

In C#, "static" merely means that you can call the method directly from the class definition as opposed to calling it against an instance of the class.

Think of the "class" as the blueprint for an object. When you create an instance of an object, the blueprint tells the computer how to create it. There are, however, some aspects of the class that don't require an actual object instance, and those aspects are candidates to be provided via "static" methods.

If you change the declaration of your method to include 'static', it changes the way it must be called.

public class Example
{   
    static void StaticMethod() {...};  // object instance *not* required
    void InstanceMethod() {...};       // object instance *is* required
}
David W
  • 10,062
  • 34
  • 60