abp icon indicating copy to clipboard operation
abp copied to clipboard

Dapper Integration: GetDbConnectionAsync() doesn't respect CommandTimeout Settings from DbContext

Open alexkleinwaechter opened this issue 2 years ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Description

On my Dapper Repository the method GetDbConnectionAsync() always uses the default command timeout = 30 seconds.

In my factory I setup the DbContext like that:

var builder = new DbContextOptionsBuilder<SimonDbContext>()
      .UseSqlServer(configuration.GetConnectionString("Default"),
      options => {
          options.CommandTimeout((int)TimeSpan.FromMinutes(10).TotalSeconds);
          options.EnableRetryOnFailure();
          });

I even tried it on DbContext constructor:

public SimonDbContext(DbContextOptions<SimonDbContext> options)
    : base(options)
{
    Database.SetCommandTimeout((int)TimeSpan.FromMinutes(10).TotalSeconds);
}

I need a higher command timeout in some cases. Since

var dbConnection = await GetDbConnectionAsync();

returns an established connection the command timeout is writeprotected.

I'm not sure if this is a bug but I can't figure it out a solution myself.

Thanks for your help! Alex

Reproduction Steps

  • Set a higher command timeout on dbcontext creation
  • check the command timeout on a Dapper connection created with GetDbConnectionAsync

Expected behavior

The command timeout from dbcontext should be used.

Actual behavior

The default command timeout of 30 seconds is used.

Version

7.4

alexkleinwaechter avatar Oct 17 '23 15:10 alexkleinwaechter

hi

https://github.com/dotnet/efcore/issues/6246

maliming avatar Oct 18 '23 05:10 maliming

I just found out that you can add the command timeout as parameter to the command method: return (await dbConnection.QueryAsync<ExportListDto>( sql, transaction: await GetDbTransactionAsync(), commandTimeout: 300, param: parameters ) ).ToList();

alexkleinwaechter avatar Oct 23 '23 10:10 alexkleinwaechter

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Apr 27 '25 03:04 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 26 '25 23:06 stale[bot]