c# - NPGSQL 开始文本导入在较大的 CSV 文件上引发异常
问题描述
编辑:经过更多测试后,此错误与 HAPROXY 直接相关。当我把它从堆栈中取出时,它按预期工作。
我已经为这个问题苦苦挣扎了好几天。此代码适用于较小的 CSV 文件 (25 KB),但一旦您尝试处理较大的文件 (5000 KB),它就会引发异常。
我的堆栈是从 asp.net core 3.0、NPGSQL 4.1.1 到 HAPROXY 到 PostgreSQL 12 实例的以下代码。
这是代码:
string query = "COPY " + seedingDataApi.tableName + " FROM STDIN CSV ENCODING 'UTF8' QUOTE '\"'; ";
using (var conn = postgresContext.GetDBConnectionPG())
{
conn.Open();
using (var writer = conn.BeginTextImport(query))
{
_logger.LogInformation("Text import started");
using (StreamReader sr = new StreamReader(seedingDataApi.tableName + ".csv"))
{
_logger.LogInformation("Read stream started.");
while (!sr.EndOfStream)
{
string testString = "";
testString = sr.ReadLine();
while ("<EOL>" != testString.Substring(Math.Max(0, testString.Length - 5)))
{
testString += sr.ReadLine();
}
testString = testString.Remove(testString.Length - 5);
writer.WriteLine(testString);
}
}
}
}
应用程序抛出的异常是:
System.Exception: Npgsql.NpgsqlException (0x80004005): Exception while reading from stream
---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
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.NpgsqlConnector.ReadMessage(DataRowLoadingMode dataRowLoadingMode)
at Npgsql.NpgsqlRawCopyStream..ctor(NpgsqlConnector connector, String copyCommand)
at Npgsql.NpgsqlConnection.BeginTextImport(String copyFromCommand)
at MiniAppApiServer.Repositories.TableSeedRepo.TableSeedRepository.SeedTableDataAsync(TableSeedDataParams seedingDataApi, CancellationToken ct) in /source/MiniAppApiServer/Repositories/TableSeedRepo/TableSeedRepository.cs:line 306System.IO.EndOfStreamException: Attempted to read past the end of the stream.
at Npgsql.NpgsqlReadBuffer.<>c__DisplayClass34_0.<<Ensure>g__EnsureLong|0>d.MoveNext()
at MiniAppApiServer.Repositories.TableSeedRepo.TableSeedRepository.SeedTableDataAsync(TableSeedDataParams seedingDataApi, CancellationToken ct) in /source/MiniAppApiServer/Repositories/TableSeedRepo/TableSeedRepository.cs:line 339
at MiniAppApiServer.Supervisors.TableSeedSupervisor.TableSeedSupervisor.SeedTableDataAsync(Stream data, String tablename, CancellationToken ct) in /source/MiniAppApiServer/Supervisors/TableSeedSupervisor/TableSeedSupervisor.cs:line 86
at MiniAppApiServer.Controllers.TableSeedingController.PostTableDataSeed(IFormFile file, CancellationToken ct) in /source/MiniAppApiServer/Controllers/TableSeedingController.cs:line 114
非常感谢!
解决方案
解决方案达成!问题出在 HAProxy 配置上。我更改了以下值:
timeout client 1m
timeout server 1m
对此:
timeout client 60m
timeout server 60m
推荐阅读
- terraform - 如何在 terraform cloud 中从 git 加载文件?
- objective-c - 在视图控制器之间快速导航
- python - 从与另一个数据帧重叠的数据帧中删除行
- sql - 识别安装后对 SQL 数据库结构的更改
- mysql - MySQL:用单个查询在整个表中用实际 NULL 值替换“NULL”作为字符串
- asp.net - 如何在 ASP.NET Core MVC Web 应用程序项目中本地化数据库中的数据
- node.js - MongoDB中的数据库池在集合级别?
- angular - 如何防止在构建期间禁用和启用 Angular 应用程序中的按钮
- c - 指针乘以变量值,var = (var + 1)&3
- android - 仅在使用自动链接时忽略 android 的 react 本机库