3
I am using the following function to connect to the database, I am programming in C# in Visual Studio 2013.
namespace WindowsFormsApplication1
{
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
{
conn.Open();
return conn;
}
}
}
catch (Exception excep)
{
MessageBox.Show("Erro - " + excep.Message);
return null;
}
}
public static void closeConnection()
{
try
{
conn.Close();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message);
}
}
}
}
My idea of how to use this function would be, the connection only open, perform the necessary operations and then close the connection. But I’m having some problems.
When using this connection model the pool Firebird connection will only be with 1 active connection, or when I open the connection and close it the pool increases?
I created in Firebird an example table with auto increment of the primary key using Trigger, but it is increasing 2 by 2, when I open and close the connection, if I insert two items without closing the connection it increases 1 by 1. What can cause this?
Just out of curiosity. Why don’t you use an ORM?
– Jéf Bueno
The connection pool is not of Firebird but of ADO.NET. In addition, I could not understand the problem. Try reviewing the score on your question and also try to inform the consumer code of this class. Finally, what you are doing seems too much and unnecessarily complex. How about just make a method that always delivers a new connection object and leave with consumer code the responsibility to discard the connection (which is very simple using the block using)?
– Caffé