实体框架长超时从Azure PostgresSQL数据库查询



我在调用Azure PostgresSQL服务器时遇到过长的超时问题。在极少数情况下,对数据库的调用将在超时前花费大约15分钟。从Azure Portal中我们可以看出,查询实际上甚至还没有开始。下面是其中一个查询的示例:

System system = await (from r in _posSystemManagerContext.System
where r.StoreNumber == storeNumber
&& r.MacAddress.ToLower() == macAddress.ToLower()
select r).SingleOrDefaultAsync();

这个查询一直在发生,通常在不到100ms的时间内完成。其他服务的其他查询也都在此时进行,没有问题。但是这个(以及其他几个)将随机得到如下错误:

Failed executing DbCommand (944,756ms) [Parameters=[@__storeNumber_0='?' (DbType = Int32), @__ToLower_1='?'], CommandType='Text', CommandTimeout='30'] SELECT r."StoreNumber", r."MacAddress", r."AddedBy", r."DateAdded", r."DateLastModified", r."LastAccess", r."LastModifiedBy", r."SystemNumber", r."State" FROM "System" AS r WHERE (r."StoreNumber" = @__storeNumber_0) AND (LOWER(r."MacAddress") = @__ToLower_1) LIMIT 2
An exception occurred while iterating over the results of a query for context type 'PosSystemManager.Data.Contexts.PosSystemManagerContext'.
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. --->
Npgsql.NpgsqlException (0x80004005): Exception while reading from stream --->
System.IO.IOException: Unable to read data from the transport connection: Connection timed out. --->
System.Net.Sockets.SocketException (110): Connection timed out
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.Security.SslStream.<FillBufferAsync>g__InternalFillBufferAsync|215_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)
at System.Net.Security.SslStream.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
at Npgsql.NpgsqlReadBuffer.<>c__DisplayClass34_0.<<Ensure>g__EnsureLong|0>d.MoveNext()
at Npgsql.NpgsqlReadBuffer.<>c__DisplayClass34_0.<<Ensure>g__EnsureLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(DbContext _, Boolean result, CancellationToken cancellationToken)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()

我们的连接字符串只有主机,端口,用户,密码,Ssl模式(需要)和最大池大小(10)。

应用程序是。net Core 3.1,与Npgsql.EntityFrameworkCore.PostgreSQL版本3.1.4。该数据库是一个8 vCore的通用Azure数据库,用于运行11.12版本的PostgresSQL。

当默认连接超时为15秒,默认命令超时为30秒时,您知道为什么会挂起15分钟吗?任何帮助都会很感激。谢谢你。

在应用程序升级到。net 6和Npgsql 6.0.4之后,所有超时(超时的原因是一个完全不同的问题)现在只需要30秒。

最新更新