DapperLib / DapperLib/Dapper

NullReferenceException when first query

Open
#2,090 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

I'm using Dapper to connect to a MySQL database for data queries. The project runs fine locally, but when I deploy it to AWS EKS, the service starts up normally. However, when I start making requests to the service's gRPC interface for data queries, I get a NullReferenceException on the first request to the same interface, but subsequent requests are fine.
When I wait for a longer period of time and then make a request to the same interface again, I occasionally get the NullReferenceException again.

Here are some information of env:
Mysql: 8.0.35

<PropertyGroup>
	<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
	<PackageReference Include="Dapper" Version="2.1.35" />
	<PackageReference Include="MySql.Data" Version="8.4.0" />
</ItemGroup>

connectionString: Server=xxx;port=3306;user=xx;password=xxx;database=xxx;CharSet=utf8mb4;pooling=true;

At first, I suspected that it was an issue with the connection pool initialization, so I tried the following methods, but they didn't work:

using (var conn = GetConnection())
{
    conn.Open();
    conn.Close();
}

The exception log is below:

System.NullReferenceException: Object reference not set to an instance of an object.
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean execAsync, CancellationToken cancellationToken)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(Boolean execAsync, CancellationToken cancellationToken)
at Dapper.SqlMapper.ExecuteMultiImplAsync(IDbConnection cnn, CommandDefinition command, IEnumerable multiExec) in /_/Dapper/SqlMapper.Async.cs:line 640
 System.NullReferenceException: Object reference not set to an instance of an object.
    at MySql.Data.MySqlClient.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean execAsync, CancellationToken cancellationToken)
    at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(Boolean execAsync, CancellationToken cancellationToken)
    at Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, Object param) in /_/Dapper/SqlMapper.Async.cs:line 662
    at Dapper.DapperExtensions.ExecuteAsync(IDbConnection conn, ExCommand[] cmdJoined) in /src/TradingApp.Data/Extensions/Dapper/DapperExtensions.cs:line 75

Any suggestion here?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the intermittent failure and inspect MySql.Data.MySqlClient.MySqlCommand.ExecuteReaderAsync alongside Dapper's SqlMapper.Async.cs lines 640 and 662. Then review the caller at DapperExtensions.cs line 75 and the connection lifecycle; done means identifying a reproducible cause and confirming that the first and delayed requests no longer throw.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, mysql
Domain
api, backend, databases
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.