3

I'm developing a project using asp.net c# webforms framework 4.5, and I did a connection test on Firebird database, but when I close this connection it isn't closing, I used the following code to do it:

string conDDNS;
FbConnection conexaoDDNS;

protected void Abrir_Fechar_Click(object sender, EventArgs e)
{
    try
    {
        this.conDDNS = "DRIVER=InterBase/Firebird(r) driver;User=SYSDBA;Password=masterkey;Database=localhost:C:/AdCom/ADCOM.FDB";

        this.conexaoDDNS = new FbConnection(conDDNS);
        this.conexaoDDNS.Open();
        ListItem item = new ListItem("Conexão aberta");
        ListBox1.Items.Add(item);

        this.conexaoDDNS.Dispose();
        this.conexaoDDNS.Close();
        ListItem item2 = new ListItem("Conexão fechada");
        ListBox1.Items.Add(item2);

    }
    catch (Exception erro)
    {
        ListItem item = new ListItem(erro.ToString());
        ListBox1.Items.Add(item);
    }

}

I have already used the .Close() and .Dispose() command but it didn't work.

When I did this debugging I realized that when it pass by .Open()command it opens the connection, that is alright. But when it pass by .Close() command the connection is still open on database.

To know the number of connections opened on database I'm using the following command:

select * FROM MON$ATTACHMENTS
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Lucas Sousa
  • 141
  • 2
  • 5
  • You should call Close() before you call Dispose(). You could also use a Using-Statement instead of calling Dispose manually. Are you sure your check for open connections, always is the current state or maybe its updated a bit delayed? How long the connection stays open after you called close? – BoeseB Feb 15 '16 at 12:18
  • 2
    The Firebird .NET provider has a built-in connection pool; I believe it is enabled by default, add `Pooling=false` to the connection string to disable it. Also make sure you start a new transaction when querying `MON$ATTACHMENTS`. The content of monitoring tables is 'frozen' inside a single transaction. – Mark Rotteveel Feb 15 '16 at 15:26
  • @MarkRotteveel it worked adding `Pooling = false`, thank you so much ! – Lucas Sousa Feb 15 '16 at 17:05

1 Answers1

10

The Firebird .NET provider has a built-in connection pool and it is enabled by default. You can add Pooling=false to the connection string to disable it. However in a lot of cases a connection pool is a good thing (it saves the time of having to open a connection), so make sure you really need to disable it.

Calling FbConnection.ClearPool(connection) or FbConnection.ClearAllPools() should close currently open connections in the pool.

Also make sure you start a new transaction when querying MON$ATTACHMENTS. The content of monitoring tables is 'frozen' inside a single transaction.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197