views:

176

answers:

3

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

+10  A: 

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.

Mehrdad Afshari
ok, but then list of active connections in activity monitor increase dramatically and sometimes pooling does not handle all these connections. After what time connections (with status sleeping) are disposed?
niao
@niao: I'm not sure about a timeout for releasing connections. If you're worried about the number of connections, you can set the maximum pool size in the connection string. You can also clear the pool manually from time to time by calling the `ClearPool` method. See: http://msdn.microsoft.com/en-us/library/8xx3tyca(VS.80).aspx
Mehrdad Afshari
dear Mehrdad Afshari how can i set timeout for releasing connections?
niao
As I said, I don't think connections are released on a timely basis (not sure though). I suggest not to interfere manually with the pooling process unless it's actively causing problems for you. However, if you really want connections to get released on a schedule, use a timer to run a the `ClearPool` or `ClearAllPools` method manually in your application.
Mehrdad Afshari
+3  A: 

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.

AdaTheDev
To be pedantic, with MARS, a single connection can be reused.
Mehrdad Afshari
@Mehrdad - good point, I probably should have said about that.
AdaTheDev
+1  A: 

Putting the sql connection aside...

This singleton pattern is not thread safe and is a bad idea to use in a multi-threaded application (as your WCF service is likely to be).

With this code, if multiple simultaneous requests arrive, it is possible that multiple instances will be created.

David B