Pros and Cons of using Singleton Pattern in DAL

前端 未结 6 1592
感动是毒
感动是毒 2021-02-06 16:26

I have asked to use singleton pattern implemented DAL, but I think its difficult to pool the connections,use transactions..etc

I would like to know the pros and cons and

6条回答
  •  悲&欢浪女
    2021-02-06 17:17

    I don't think you'll have performance differences if you use a singleton or not, because you still can have multiple threads running on the same method at the same time. If you take care of not having internal fields that will be shared in all threads, everything will work well.

    At the end, the class that manages the connection pool needs to be thread safe and you will end up making a few locks that could affect the performance, but they are all needed. (it's made internally in the framework, and you cannot change it's behavior anyways)

    If you decide to do not use a singleton, be sure that your DAL instances are lightweigth because this might make a difference, but usually it is not.

    Note: talking about the connection pools, the only important thing you must take care is of following the "open late, close early" pattern. That means, delay the open of the connection as much as possible, and close it ASAP after you done all you need with it.

    After having all the system built using this magic rule, you can play with the connection string parameters to change some pool options (initial size, max size, ...)

提交回复
热议问题