SqlException (Timeout) when reading data with EF Core while writing data with Dapper

0

We are having a quite large ASP.NET Core 2.0 application which uses dapper to write data and EF Core to read data. This decision was made some years ago due to performance issues when writing data with EF Core.

The following sample code is used to write data:

using (var dbContext = new MyDbContext(dbContextOptions)
{
    using (var dbConnection = dbContext.Database.GetDbConnection())
    {
        await dbConnection.OpenAsync();
        using (var transaction = dbConnection.BeginTransaction())
        {
            foreach (var batch in groupedEntities)
            {
                string statement = this.createSqlStatement.ForInsert(
                        // ...
                    );

                await dbConnection.ExecuteAsync(
                    statement,
                    batch.Value,
                    transaction);
            }

            transaction.Commit();
        }
    }
}

Now we observed, that while the write-transaction from the code above is running, the following sample code to read data throws a timeout-exception (SqlServer):

using (var dbContext = new MyDbContext(this.dbContextOptions))
{
    var myData = await tkqDbContext.Set<MyEntity>().Where(e => e.Id == myId).ToListAsync();
    // ....

    return myData;
}

The Exception:

An unhandled exception has occurred: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out
    at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__122_0(Task`1 result)
    at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)

So, does anybody know the cause for that timeout? When no write operation is processed, the code for reading data works just fine.

c#
sql-server
asp.net-core-2.0
dapper
ef-core-2.2
asked on Stack Overflow Feb 15, 2021 by oopbase

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0