下面是使用来自PostgreSQL代码的npgsql的.Net大容量插入的代码。
try
{
var items = GetSourceData(task);
connection.Open();
var command = new NpgsqlCommand(null, connection);
BeforeDestinationCommandExecution(task, command);
command.CommandText = string.Format("COPY {0} FROM STDIN", task.DestinationTable);
command.CommandTimeout = 3600;
var cin = new NpgsqlCopyIn(command, connection);
var rowCount = 0;
try
{
cin.Start();
foreach (var item in items)
{
var b = StreamEncoding.GetBytes(ConvertSourceData(item));
cin.CopyStream.Write(b, 0, b.Length);
++rowCount;
}
cin.End();
log.Debug(string.Format("Table {0} contained {1:N0} records", task.DestinationTable, rowCount));
}
catch (Exception e)
{
log.ErrorException("Exception caught in inner try block - MigrateWithCopyMode", e);
try
{
// send CopyFail to server
cin.Cancel("Undo copy");
}
catch (Exception cancelException)
{
// we should get an error in response to our cancel request:
if (!cancelException.ToString().Contains("Undo copy"))
{
throw new Exception("Failed to cancel COPY: " + cancelException + " upon failure: " + e);
}
}
throw;
}
finally
{
_migrationCounts.Add(task.DestinationTable, rowCount);
}在过去的两天里,我在执行代码时遇到了未处理的异常。经过一些调查并将代码附加到UnhandledException事件中。System.AppDomain.CurrentDomain.UnhandledException += unhandledException; --我发现问题出在数据处理中。
Error [16] [HubAdapterMsSqlPostgres] Unhandled exception Npgsql.NpgsqlException:
null value in column "name_ru" violates not-null constraint
Severity: ERROR
Code: 23502
at Npgsql.NpgsqlState.<ProcessBackendResponses_Ver_3>d__a.MoveNext()
at Npgsql.NpgsqlState.IterateThroughAllResponses(IEnumerable`1 ienum)
at Npgsql.NpgsqlConnector.NpgsqlContextHolder.ProcessServerMessages()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()此错误是在不同线程上生成的。我想,在从前面的代码示例调用cin.CopyStream.Write(b, 0, b.Length);之后。
我的问题是
建议的处理此类错误的方法可以省略值错误的行并在大容量插入操作中继续
谢谢
发布于 2017-02-05 10:47:28
您正在尝试导入包含与表定义不兼容的行的数据--它们缺少非空列的数据。复制是一个全过程或无过程:要么整个过程成功,要么完全失败。因此,您必须首先清除输入,删除违规行(这将涉及解析输入,这可能很困难),或者*临时删除表上的非空约束,执行副本,从表中删除有问题的行并恢复约束。这可能合适,也可能不合适,这取决于您的使用场景。
不管怎么说,您使用的是Npgsql 2.x,它现在已经很老了,没有维护。强烈建议您升级到最新版本的Npgsql。
https://stackoverflow.com/questions/42050953
复制相似问题