[英]Postgresql fails specific query ONE time after Windows reboot
我在Windows上使用Postgresql在C#應用程序中。 我遇到的問題非常奇怪,可以描述如下:
SELECT COUNT(*) AS c FROM files WHERE total_bytes IS NOT NULL
奇怪的說明:
SELECT COUNT(*) AS c FROM files
異常追溯:
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文檔中的解決方案。
默認情況下,Npgsql附帶一些連接和命令的超時參數。 Windows重新啟動后,首先訪問該表非常慢,觸發命令超時。
通過連接字符串上的其他參數,我可以更高地更改這些設置並解決我的問題:
connectionString += ";Timeout=180;Command Timeout=180";
獎金提示 :Postgresql函數pg_sleep(seconds)
幫助我在沒有實際重啟的情況下重現問題。 很有幫助:
SELECT pg_sleep(60);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.