Firebird C# create a single connection to the database

Asked

Viewed 680 times

0

I need to create in my application only a connection using the fbconnection that will be shared across the application.

I performed this procedure as follows.

I created a static class that provides me the connection to the BD:

static class Conexao
{


    private static String strConn = Properties.Settings.Default.caminhoFbConnection;
    private static FbConnection conn = null;


    public static void Conection()
    {

    }

    public static FbConnection getConnection()
    {
        try
        {

            if (conn == null)
            {
                conn = new FbConnection(strConn);
                conn.Open();
                return conn;
            }
            else
            {
                if (conn.State == System.Data.ConnectionState.Open)
                {
                    return conn;
                }
                else
                {
                    FbConnection.ClearAllPools();
                    conn = new FbConnection(strConn);
                    conn.Open();
                    return conn;
                }
            }
        }
        catch (Exception excep)
        {
            MessageBox.Show("Erro - " + excep.Message);
            return null;
        }
    }
}

and every time I need a connection I simply call the function Conexao.getConnection();

My question is this: do you have any problem using and or creating the connection in a static way and using it throughout the program run? Or it can overload and stop working in the middle of execution?

  • you are wanting to implement the Singleton standard for a database connection?

  • It can be said that yes, I have the Firebird server on a computer and several clients that use this connection to connect to it, I would like that each client uses only one connection to not overload the server.

  • Why don’t you add the question meaning that? In my opinion the question would become more interesting. Trying how to put Use Singleton Pattern on database connections? Sure it would rain of answers.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.