I'm using Essential.Logging's SQL logger. I looked at the source of the library here and saw (at the end)
Everytime you want to log to SQL:
using (var connection = DbProviderFactoryExtensions.CreateConnection(dbFactory, connectionSettings.ConnectionString))
{
using (var command = DbProviderFactoryExtensions.CreateCommand(dbFactory, CommandText, connection))
{
command.Parameters.Add(DbProviderFactoryExtensions.CreateParameter(dbFactory, "@ApplicationName", ApplicationName != null ? (object)ApplicationName : DBNull.Value));
// ..snip..
command.Parameters.Add(DbProviderFactoryExtensions.CreateParameter(dbFactory, "@Data", dataString != null ? (object)dataString : DBNull.Value));
connection.Open(); // <====
command.ExecuteNonQuery();
}
}
The helper function right on the top of the using statement is
public static DbConnection CreateConnection(DbProviderFactory dbFactory, string connectionString)
{
if (dbFactory == null) throw new ArgumentNullException("dbFactory");
var connection = dbFactory.CreateConnection();
connection.ConnectionString = connectionString;
return connection;
}
Question:
- Is it opening (and closing) the connection to the SQL server everytime it does that? I assume it would be more efficient to pool the connection to the SQL server, right?
- If I were to replace that with Entity Framework 5.0 (with a
using (var db = new myDbContext()) { }
) would that result in connection pooling?