Question

In my windows application I am using Access database. When application starts it should to open the connection. I use the connection string to call the procedures/stored queries and access the data. The connection should open till the application exits.

I was asked to use singleton class. Please help me how to use singleton class for database connection.

Was it helpful?

Solution

To use a singleton directly, you'd have to inherit from the connection object, which isn't possible with an OleDbConnection (as it is sealed). So you'd have to write a class, with a public property that exposes the connection itself, and make that wrapper class a singleton.

However, it the only goal is to have one instance of the connection, you could use a 'Lazy' construction, which is thread safe (For that matter, even in the singleton, I'd use Lazy or LazyInitializer to be prepared for threading)

public static class Data    
{
    public static readonly Lazy<OleDbConnection> Connection = new Lazy<OleDbConnection>(CreateConnection); //(using System.Threading)

    static OleDbConnection CreateConnection()
    {
        var conn = new OleDbConnection("YourConnectionString");
        //etc
        conn.Open();
        return conn;
    }
}

getting the connection would be something like:

var conn = Data.Connection.Value;

Then again, if you would incorporate a class that encapsulates all calls to the connection, that would be a perfect case for a singleton

* edit ** An example using a single connection, with reset*

    public static class Data   
{
    static OleDbConnection conn;
    public static OleDbConnection Connection
    {
        get
        {
            if (conn == null)
                LazyInitializer.EnsureInitialized(ref conn, CreateConnection);
            return conn;
        }
    }

    static OleDbConnection CreateConnection()
    {
        if (strDataFilePath == null)
            throw new Exception("Datafile paths is not set");
        //build connection, using strDataFilePath
        var conn = new OleDbConnection("YourConnectionStringWithDataFilePath"); 
        //other settings

        //open
        conn.Open();
        return conn;
    }

    static string strDataFilePath;

    public static string DataFilePath
    {
        get { return strDataFilePath; }
        set
        {
            if(strDataFilePath==value)return;
            strDataFilePath = value;
            if(conn!=null){
                conn.Close(); //NB, no checks were added if the connection is being used, but if the value is only set on startup or idle moments, this should be ok for the example.
                conn.Dispose();
                conn=null; //conn is reset, and (re)created the next time Connection is called
            }
        }
    }
}

And initializing:

Data.DataFilePath = ".....";

Using the connection:

var conn = Data.Connection; //Connection is created and opened on first call
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top