I want to create a static class for working with a database containing methods for getting data. But the fact is that calls to class methods can come from different streams.

Question. How does a similar class behave while simultaneously accessing one of the methods from two different threads? (Will he wait until the first request is completed, will an exception occur, or will the request be executed in another thread?)

More precisely: How to properly organize queries to the database by executing them from different threads?

    1 answer 1

    Putting a static class in such a task, as a foundation for working with a database, means abandoning the PLO and proceeding to procedural programming, gradually building up the trash bin, at best divided into helper classes, datasors or something. Moreover, the "context" of the database is usually NOT thread-safe, like static, and you will have to constantly take care of synchronization. It is much more efficient to use any ORM to work with the database + ( UnitOfWork + Repository + Specification ). In this approach, the architecture will also be tested, which cannot be said about continuous static classes and methods, which can only be tested with the help of additional code in the form of "wrappers". To manage the life cycle of objects, it makes sense to use one of the existing DI / IoC solutions. For example, in your case, you can specify at the configuration stage the life cycle for the database context "PerThread" and "container" will ensure that each stream has its own, new context or one transaction.

    • I don’t know whether to use ORM or Dependency Injections per thread to process a request. In my task, requests will be read, and this is usually thread-safe. And the MSDN SqlConnection itself: "Any public members of this type declared to be static are thread safe. The thread safety of instance members is not guaranteed." I think that a global connection will be enough to work with it from different threads. I just do not want to block , to achieve the most effective work from the application. I hope that you still tell me. - michael
    • one
      I understand you want to use a minimum of effort and get maximum performance. In addition, requests are limited to samples only - already easier. In such a scheme, ONE connection for all threads will be quite a working approach, that is, all threads will access the database within the ONE connection. If that suits you, go ahead. But if all of a sudden you decide to add data modification operations here, then you can’t avoid unexplainable results of the program’s work (I suspect there will be errors associated with access to the same data at the database level). - wind
    • one
      By the way, it follows from your quote from msdn, which I said - the database context is NOT thread-safe. - wind
    • In general, the documentation dealt with static members of the SqlConnection class itself, and not with static SqlConnection instances. With thread safety, I guarantee problems when reading data using DataReader . - Modus
    • Do you have experience? What do you propose to use? - Michael