Access to the database through a static class from a number of threads

I am a C # programmer, I missed some snippets here and there when it comes to the fact that you are very good at things, and now I came across something that I could not find to answer the SO. I'm trying to better understand thread safety in C #, but let me first point out the context.

I am currently developing a Windows service that shuts down and does some monitoring work based on the schedule that is in the SQL Server database. It will track some servers, making http requests to several "client servers", the client installed on these servers will respond with the requested information.

Since this monitoring service may be quite busy, I configured it to use each "scheduled instruction" in a new thread when it is scheduled to do this work. This is done so that my timer continues to tick in a good way, ready to release the next command to the next "client server".

Part of each command is that it must be registered in a database that was executed successfully, and what was the answer, and so on. Now I have a public static class Logger in the monitoring service, I think this is convenient, because now I can easily call it that Logger.Log(... ) whenever I need to write things. This logging occurs in this class through EF to the SQL Server database.

It all sounds very cool to me, and I'm quite happy with how it all works, but so far I have not tested anything. The problem that I have in all of this is that my brain tells me that since my logger class is static and, in my understanding, it is created only once? - if more than 1 thread tries to call Logger.Log(.. ) at the same time, bad things will happen to my monitoring service.

Is there anyone here who can enlighten me? I think right or wrong? And if you know the answer, please explain it clearly, because I would like to understand it. :)

Update:

Thanks for the answers so far, everything is becoming clearer, as people ask for more information about the Log method, and I am not on my development PC at the moment, I will try to explain how it works in more detail.

The whole Log method is to add an entry to the SQL database through EF based on data from some previously created objects that are passed to the method as parameters. The database context is created as a static private variable in a static class. The reason for this is that I do not need to continue to use expressions in my overloads.

+7
multithreading c # static thread-safety class
source share
3 answers

Each method, whether static or virtual, will have its own frame, so there is no problem with the stream. The problem arises in the implementation of the method: some static methods will use static variables or static resources, and they are all the same, and you will encounter race conditions. But local variables declared inside a static method are not static, so if your method does not modify static variables or resources, you will be fine.

+3
source share

static provides an opportunity for dangerous code, but it does not guarantee it. If you use the static class / method, you must be careful not to use instance data.

What does this mean in your case? Basically, you want to create an instance of DbContext as part of the Log method, do the registration and Dispose DbContext (finish using the using statement). As long as there is no sharing of instance data , everything will be fine.

However, if you do something in a static constructor or use class-level variables, you can create problems.

Change In your particular case, you should not share DbContext into all of your threads. DbContext look here for a discussion of the correct scope for DbContext . It must be created in every method.

This blog entry says the following (and clarifies):

Most [of these considerations] tend to indicate a short-lived context that is not common.

So this is my recommended rule.

+3
source share

What does the documentation for your Logger class say regarding thread safety? By a static class or method, there is nothing inherently unsafe.

If you call a method or property, static or not

  • only refers to verifications belonging to this method (for example, does not refer to any instance or static elements) and
  • creates its own instances of any other classes with which it needs to collaborate

You must be thread safe. Note that any methods or properties invoked in other classes must also be thread safe.

+3
source share

All Articles