You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core用Dapper操作Azure SQL偶发传输层错误,已关AUTO CLOSE如何解决

问题背景

我通过.NET Core 2.2控制台应用程序对Azure上的SQL数据库执行操作,使用Dapper的代码如下:

using (IDbConnection db = new SqlConnection(_appSettings.ConnectionStrings))
    db.Execute("delete from MyTable where Id=@id", new { id = myObject.Id });

一天内仅会极少次数触发报错。

报错信息

A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 19 - Physical connection is not usable)

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error) at System.Data.SqlClient.TdsParserStateObject.ReadSniSyncOverAsync() at System.Data.SqlClient.TdsParserStateObject.TryReadNetworkPacket() at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer() at System.Data.SqlClient.TdsParserStateObject.TryReadByte(Byte& value) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, CommandDefinition& command, Action2 paramReader) in C:\projects\dapper\Dapper\SqlMapper.cs:line 2827 at Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, CommandDefinition& command) in C:\projects\dapper\Dapper\SqlMapper.cs:line 570 at Dapper.SqlMapper.Execute(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 443
at ProjectQueue.Engine.ExecuteAsync(CancellationToken stoppingToken) in C:\Users\userX\source\repos\myProject\ProjectQueue\Engine.cs:line 75

已尝试方案

已将SQL数据库的AUTO CLOSE设置为false,对应配置如下:
AUTO CLOSE配置截图

可能的诱因
  • Azure云网络或服务端偶发抖动:Azure SQL存在常规的运维升级、网络链路波动、网关空闲连接回收机制,低频次触发时大概率是这类瞬态故障导致服务端主动断开了连接。
  • 连接池留存无效连接:连接池内缓存的连接已经被服务端断开,但客户端未感知到,首次取出使用时就会触发物理连接不可用的错误。
  • 旧版SqlClient驱动缺陷:.NET Core 2.2配套的System.Data.SqlClient驱动存在已知的连接管理bug,对服务端断开连接的场景处理不完善。
  • 连接字符串配置不合理:未配置连接重试参数,或者超时时间设置过短,遇到瞬时波动直接抛出错误。
排查与解决方法
  • 新增瞬态故障重试逻辑:使用Polly等重试框架,针对传输层错误、服务端临时不可用等瞬态SqlException添加重试策略,设置3-5次重试,每次间隔2-10秒,绝大多数偶发的此类故障可通过重试直接解决。
  • 替换数据访问驱动:将旧的System.Data.SqlClient替换为微软官方维护的Microsoft.Data.SqlClient最新稳定版,该驱动修复了大量连接管理、传输层相关的已知问题。
  • 优化连接字符串配置:添加ConnectRetryCount=3、ConnectRetryInterval=10、Connection Timeout=30参数,可根据业务需要设置Min Pool Size=5,减少连接池空负载时的新建连接开销。
  • 排查Azure SQL运行指标:登录Azure门户查看对应数据库的连接失败日志、CPU/IO/内存使用率指标,确认是否存在服务端限流、资源占满或者计划内运维事件导致的主动断连。
  • 定期清理无效连接:如果是长期运行的后台服务,可以每间隔数小时调用一次SqlConnection.ClearAllPools(),主动清理连接池内已经失效的连接。

内容的提问来源于stack exchange,提问作者markzzz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 04:36:02