Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlConnection Singleton

Greetings, I would like to ask if creating Singleton to have only one active connection to db is a good idea. What i would like to do is: 1) I have a wcf service 2) wcf service gets data from db 3) i would like to create a singleton like this to have only one connection to db:

private static PersistanceSingleton _Instance;
    public static PersistanceSingleton Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = new PersistanceSingleton();
            }
            return _Instance;
        }
    }

I know this is not an ideal singleton but i just wrote it for this post purpose. I would like to have some persistance repositories here and which I will be instantiating them in constructor. Inside my service class I would have the following code inside constructor

_DBPersistanceSingleton = PersistanceSingleton.Instance;

Then when some request comes (e.g. GetUsersRequest) i would like to do something like:

_DBPersistanceSingleton.GetUsers()

Before each call to db is executed I will also check whether SqlConnection is open or not. Please let me know if this is a good practice. The reason why I think about this solution is because of large number of users that will be connecting to that service via client application

like image 596
niao Avatar asked Mar 04 '10 19:03

niao


People also ask

Should SqlConnection be Singleton?

Using a Singleton for a SqlConnection object is a really, really bad idea. There is no reason to do this whatsoever. If you are attempting to avoid a performance hit of "new SqlConnection()" or "connection.

Should DB class be Singleton?

A DB connection should not normally be a Singleton. Two reasons: many DB drivers are not thread safe. Using a singleton means that if you have many threads, they will all share the same connection.

What is SqlConnection in Ado net?

It is used to establish an open connection to the SQL Server database. It is a sealed class so that cannot be inherited. SqlConnection class uses SqlDataAdapter and SqlCommand classes together to increase performance when connecting to a Microsoft SQL Server database.

Why should database connection be Singleton?

Singleton pattern is a good way to create a global point of access the instance. This pattern very useful when we need create a global object to reach from every action of the system. This pattern grauntied that this instance of object create one time and used for all case.


2 Answers

It's not a good practice to reuse SqlConnection like that. Open it when you need it and close it as soon as you're done with it. Connection pooling will work for you under the hood reusing the connection.

like image 63
mmx Avatar answered Sep 27 '22 18:09

mmx


No, I'd strongly recommend you don't. What happens if multiple requests come in at the same time? They can't all use the same connection at the same, at best you'd just be introducing a big bottleneck.

Connection pooling is handled automatically for you, and takes the hassle away from you so you don't need to worry about it. Just open and close connections as needed.

like image 44
AdaTheDev Avatar answered Sep 27 '22 17:09

AdaTheDev