[英]Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding with Azure SQL database
我在此实现中使用 asp.net 核心 3.1、ADO.NET、Azure SQL 数据库。 在这种情况下,使用存储过程对服务层 -> 数据库层进行 api 调用。
Azure SQL 定价说明:
通用:Gen5、8 个 vCore
代码如下:
public dynamic GetData(FiltersDto reportFilters, long typeid)
{
List<DashboardDTO> result = new List<DashboardDTO>();
DataSet ds = new DataSet();
using (var connection = new SqlConnection(_objDBContext.Database.GetDbConnection().ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "[TT].[LoadDashboard]";
command.Parameters.Add(new SqlParameter("@ReviewYear", SqlDbType.BigInt)
{Value = reportFilters.ReviewYear});
command.Parameters.Add(new SqlParameter("@Region", SqlDbType.Structured)
{Value = GetTable<int>(reportFilters.Region)});
command.Parameters.Add(new SqlParameter("@TypeId", typeid));
using (var sda = new SqlDataAdapter())
{
sda.SelectCommand = command;
sda.Fill(ds);
}
}
connection.Close();
}
if (ds.Tables.Count > 0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
// Logic to read and set the data
DashboardDTO dashboard = new dashboardDTO
{
// Fill the dto with data
}result.Add(dashboard);
}
}
return result;
}
我从 asp.net 核心 web api 调用上述方法并收到以下错误:
{
"header": "An unhandled error occurred.",
"type": "SqlException",
"errorcode": 10002,
"message": "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.",
"innerExcepioin": "The wait operation timed out.",
"stackTrace": " at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()\r\n at System.Data.SqlClient.SqlDataReader.get_MetaData()\r\n at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)\r\n at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)\r\n at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite, String method)\r\n at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)\r\n at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)\r\n at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)\r\n at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)\r\n at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)\r\n at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)\r\n"
}
我还发现平均需要 31.37s 才能得到上述错误。
谁能帮我解决这个问题的指导
您正在达到 30 秒的默认命令超时。
您正在运行的存储过程比执行该过程花费的时间更长。 所以驱动程序放弃了它的执行。
您可以设置自己的命令超时: https://learn.microsoft.com/en-us/do.net/api/system.data.sqlclient.sqlcommand.commandtimeout?view=do.net-plat-ext-6.0
command.CommandTimeout = 60; // For 60 seconds for example
您还可以检查存储过程以查看是否可以加快速度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.