views:

1517

answers:

4

I have created an singleton class, this class returns a database connection. So my question is this connection is also satisfying singleton criteria?
If no, than how I can make it singleton.
Here is the code.

public sealed class SingletonDB
{
    static readonly SingletonDB instance = new SingletonDB();
    static SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static SingletonDB()
    {
    }

    SingletonDB()
    {
    }

    public static SingletonDB Instance
    {
        get
        {
            return instance;
        }
    }

    public static SqlConnection GetDBConnection()
    {
        return con;
    }
}
+1  A: 

If there's no other way to get a connection to the DB, and if this attribute cannot be overwritten, I would say yes. If that's what you're doing, you're probably taking this singleton thing too far. What if the the DB goes down temporarily and your app loses its connection? Then you'll have to restart your app in order for it to be able to use the DB again.

allyourcode
+1  A: 

I can't answer that question without seeing some code, I think. If you are saying that you will have only one DB connection instance in your application, that might work if you can guarantee that your application will run on only one thread (or at least that all operations using the DB connection does), since you can't (as far as I know anyway) run several operations in parallell on the same connection.

Also, if it means that your application will keep the connection open in between uses, I would advise against it. DB connections are limited resources on the DB server, so you should keep them open only when they are needed, and then close them.

Fredrik Mörk
+2  A: 

The connection itself is not satisfying the Singleton criteria because you can create multiple instances of a database connection object. A singleton by definition can only be instantiated once.

You can make the SqlConnection a part of the Singleton, by changing your example to this:

public sealed class SingletonDB
{
   private static readonly SingletonDB instance = new SingletonDB();
   private readonly SqlConnection con =new SqlConnection(ConfigurationManager.ConnectionStrings["mydb"].ConnectionString);

   // Explicit static constructor to tell C# compiler
   // not to mark type as beforefieldinit
   static SingletonDB()
   {
   }

   private SingletonDB()
   {
   }

   public static SingletonDB Instance
   {
       get
       {
          return instance;
       }
   }

   public SqlConnection GetDBConnection()
   {
       return con;
   }

}

This way the SqlConnection used by your SingletonDB class would have one and only one SqlConnection, thus follow the Singleton pattern.

Michael Hedgpeth
I have changed code, check now.
Sharique
+5  A: 

Your Singleton is still off.

As far as the singleton pattern goes, please see Jon Skeet's very good and detailed description here : http://www.yoda.arachsys.com/csharp/singleton.html

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.Open()" be advised that there really is no performance hit there because of the connection pooling going on behind the scenes. Connection Pooling handles the opening/closing of the expensive connections. Not the SqlConnection object.

You won't be able to open multiple SqlDataReaders/Commands with the connection at the same time and will run into thread blocking issues if you are trying to share the same connection object with multiple threads.

The Singleton pattern is the most over used and abused pattern and there are many side effects of the singleton that you may not be aware of. Very good talk about the dangers of singletons here http://www.youtube.com/watch?v=-FRm3VPhseI

Chad Grant