DapperLib / Dapper

Dapper - a simple object mapper for .Net
https://www.learndapper.com/
Other
17.29k stars 3.67k forks source link

NullReferenceException when first query #2090

Open jtsai-dev opened 1 month ago

jtsai-dev commented 1 month ago

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?