gpt4 book ai didi

c# - Windows 重启后 Postgresql 特定查询失败一次

转载 作者:行者123 更新时间:2023-11-29 11:51:38 24 4
gpt4 key购买 nike

我在 Windows 上的 C# 应用程序中使用 Postgresql。我遇到的问题真的很奇怪,可以描述如下:

  • 我重新启动 Windows
  • 我运行程序
  • 一个特定的查询失败:SELECT COUNT(*) AS c FROM files WHERE total_bytes IS NOT NULL
  • 我再次运行程序,一切正常

奇怪的笔记:

  • 我尝试在那个查询之前进行另一个查询(甚至使用同一个表)并且它有效:SELECT COUNT(*) AS c FROM files
  • 我无法重现重新启动 Postgresql 时的错误。它只发生在 Windows 重新启动时。而且它只会发生一次。

异常回溯:

Npgsql.NpgsqlException: Exception while reading from stream

at Npgsql.ReadBuffer.Ensure(Int32 count, Boolean dontBreakOnTimeouts)
at Npgsql.NpgsqlConnector.DoReadMessage(DataRowLoadingMode dataRowLoadingMode, Boolean isPrependedMessage)
at Npgsql.NpgsqlConnector.ReadMessageWithPrepended(DataRowLoadingMode dataRowLoadingMode)
at Npgsql.NpgsqlConnector.ReadMessage(DataRowLoadingMode dataRowLoadingMode)
at Npgsql.NpgsqlConnector.ReadExpecting[T]()
at Npgsql.NpgsqlDataReader.NextResultInternal()
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.Execute(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderInternal(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Npgsql.NpgsqlCommand.ExecuteReader()
at DriveShare.Database.Postgresql.ExecuteQuery(NpgsqlCommand command) in c:\projetos\driveshareclient\DriveShare\DriveShare\Database\Postgresql.cs:line 216
at DriveShare.Database.Postgresql.Query(String sql, Object[] args) in c:\projetos\driveshareclient\DriveShare\DriveShare\Database\Postgresql.cs:line 72
at DriveShare.Database.Postgresql.QueryOne(String sql, Object[] args) in c:\projetos\driveshareclient\DriveShare\DriveShare\Database\Postgresql.cs:line 83
at DriveShare.Database.Postgresql.QueryValue(String key, String sql, Object[] args) in c:\projetos\driveshareclient\DriveShare\DriveShare\Database\Postgresql.cs:line 97
at DriveShare.Database.Postgresql.QueryValue(String key, String sql) in c:\projetos\driveshareclient\DriveShare\DriveShare\Database\Postgresql.cs:line 92
at DriveShare.Database.FileIndexDataSet.CountIndexedFiles() in c:\projetos\driveshareclient\DriveShare\DriveShare\Database\FileIndexDataSet.cs:line 89
at DriveShare.Engine.DriveShareEngine.Start() in c:\projetos\driveshareclient\DriveShare\DriveShare\Engine\DriveShareEngine.cs:line 156
at DriveShareWebService.Program.Main(String[] args) in c:\projetos\driveshareclient\DriveShare\DriveShareWebService\Program.cs:line 19

由于我必须让程序保持运行,我编写了一个解决方法,让应用在继续之前重试该查询。我并不为此感到自豪:

public void WaitForConnection()
{
int limitSeconds = 3 * 60;
var start = DateTime.Now;
while (true)
{
try
{
Log.WaitingForDatabaseConnection();
Query("SELECT COUNT(*) AS c FROM files WHERE total_bytes IS NOT NULL");
Log.DatabaseConnectionAquired();
break;
}
catch (Exception e)
{
var wastedTime = DateTime.Now - start;
if (wastedTime.TotalSeconds > limitSeconds)
throw;
else
Log.Exception(e);
}
Thread.Sleep(1000);
}
}

我正在使用 Npgsql(在一个瘦抽象类中)连接到 Postgresql。 Postgresql 日志显示了一些我还不理解的关于 winsock 错误的条目:

2016-08-16 10:14:34 BRT LOG:  database system was shut down at 2016-08-16 10:12:07 BRT
2016-08-16 10:14:34 BRT FATAL: the database system is starting up
2016-08-16 10:14:34 BRT LOG: MultiXact member wraparound protections are now enabled
2016-08-16 10:14:34 BRT LOG: sistema de banco de dados está pronto para aceitar conexões
2016-08-16 10:14:34 BRT LOG: autovacuum launcher started
2016-08-16 10:17:16 BRT LOG: could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:17:27 BRT LOG: could not send data to client: unrecognized winsock error 10054
2016-08-16 10:17:27 BRT STATEMENT: SELECT path FROM files
2016-08-16 10:17:27 BRT FATAL: connection to client lost
2016-08-16 10:17:27 BRT STATEMENT: SELECT path FROM files
2016-08-16 10:17:27 BRT LOG: could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:17:27 BRT LOG: unexpected EOF on client connection with an open transaction
2016-08-16 10:17:33 BRT LOG: unexpected EOF on client connection with an open transaction
2016-08-16 10:25:14 BRT LOG: could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:25:15 BRT LOG: could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:25:15 BRT LOG: unexpected EOF on client connection with an open transaction
2016-08-16 10:26:30 BRT LOG: could not send data to client: unrecognized winsock error 10054
2016-08-16 10:26:30 BRT FATAL: connection to client lost
2016-08-16 10:26:50 BRT LOG: could not send data to client: unrecognized winsock error 10054
2016-08-16 10:26:50 BRT FATAL: connection to client lost
2016-08-16 10:26:50 BRT LOG: could not receive data from client: unrecognized winsock error 10053
2016-08-16 10:26:50 BRT LOG: unexpected EOF on client connection with an open transaction
2016-08-16 10:27:06 BRT LOG: could not send data to client: unrecognized winsock error 10054
2016-08-16 10:27:06 BRT FATAL: connection to client lost
2016-08-16 10:27:06 BRT LOG: could not send data to client: unrecognized winsock error 10054
2016-08-16 10:27:06 BRT FATAL: connection to client lost
2016-08-16 10:27:30 BRT LOG: pedido de desligamento rápido foi recebido
2016-08-16 10:27:30 BRT LOG: interrompendo quaisquer transações ativas
2016-08-16 10:27:30 BRT LOG: autovacuum launcher shutting down
2016-08-16 10:27:30 BRT ERROR: canceling statement due to user request
2016-08-16 10:27:30 BRT LOG: autovacuum launcher shutting down
2016-08-16 10:27:30 BRT LOG: shutting down
2016-08-16 10:27:30 BRT LOG: database system is shut down

我不希望有人知道我的问题到底是什么。我只是希望有人可能有一些类似的问题可以阐明它。

最佳答案

在一些帮助下,我在 Npgsql 文档中找到了解决方案,here .

Npgsql 默认带有一些用于连接和命令的超时参数。 Windows 重启后,第一次访问表非常慢,触发命令超时。

通过连接字符串上的附加参数,我能够将这些设置更改得更高并解决我的问题:

connectionString += ";Timeout=180;Command Timeout=180";

额外提示:Postgresql 函数pg_sleep(seconds) 帮助我重现了该问题,而无需实际重新启动。非常有帮助:

SELECT pg_sleep(60);

关于c# - Windows 重启后 Postgresql 特定查询失败一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com