Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, Dapper, SQL Server and Connection Pooling

I've written some code to write some data to SQL Server, using Dapper. I don't need to wait for this write to complete before continuing other work, so want to use Task.Run() to make this asynchronous.

I have (using) statements for calling this in the rest of my system:

 using (IDataAccess ida = new DAL())
        {
            ida.WriteMessageToDB(id, routingKey, msgBody);
        }

My DAL will automatically check the dbConnection.State when the using statement is ran, and attempt a simple fix if it's closed. This works just fine for any non-async/TPL select calls.

However, when I throw a load of writes at the same time, the Task.Run() code was falling over as the connection was closed for some of them - essentially I think the parallel nature of the code meant the state was being closed by other tasks.

I 'fixed' this by doing a check to open the Connection.State within the Task.Run() code, and this appears to have 'solved' the problem. Like so:

Task.Run(() =>
            {
                if (dbConnection.State == ConnectionState.Closed)
                {
                    dbConnection.Open();
                }

                if (dbConnection.State == ConnectionState.Open)
                {
                    *Dapper SQL String and Execute Commands*
                }
            });

When I run SELECT * FROM sys.dm_exec_connections from SSMS after this, I see a lot more connections. To be expected?

Now as I understand it:

  • Dapper doesn't deal with connection pooling
  • SQL Server should automatically deal with connection pooling?

Is there anything wrong with this solution? Or a better way of doing it? I'd like to use connection pooling for obvious reasons, and as painlessly as possible.

Thanks in advance.

like image 870
Phil S Avatar asked May 09 '17 15:05

Phil S


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Thanks Juharr - I've upvoted your reply.

For reference to others, I changed write function to await and Dapper async:

private async Task WriteMessageToDB(Guid id, string tableName, string jsonString)
    {
            string sql = *Redacted*
            await dbConnection.ExecuteScalarAsync<int>(sql, new { ID = id, Body = jsonString });
    }

And then created a new task in the caller that monitors the outcome.

This is working consistently under load, and not seeing excessive new connections being created either.

like image 140
Phil S Avatar answered Oct 04 '22 13:10

Phil S