Scenario :
I have a asp.net web service that uses SQL Server 2008. It is supposed to perform some long running DB updates and it involves bulk inserting records to 3 tables in 1 request. There are approximately 220 such requests.
Flow for the a request execution is
read file having data
bulk copy into table1
call sp to process data in table1
bulk copy into table2
call sp to process data in table2
bulk copy into table3
call sp to process data in table3
Out of these 220 requests almost 1/3rd of them fail while trying to bulk load data in either table 1 or table2 with two kinds of exceptions
- column mapping does not match in source or destination
- argument out of range exception
table3 bulk load never fails if it executes
This is happening on test server. When I get the same file from test server for which I see error and debug it on my machine, everything works fine.
Also a random column mapping error does not make sense because the structure of data in file always remains the same and it does not generate error when executing from development machine.
Another observation is that it is making SQL Server memory consumption very high, so much so that eventually it starts throwing OutOfMemory exception
I have no clue where to start to narrow down the problem. Please advise.
EDIT: I tried setting batchsize for bulk uploads and also specifying option - useInternalTransaction, after that randomly I get Unexpected Existing Transaction. But if I try to bulkload the same xml from my machine, then it works fine with the same code + same DB
EDIT: 2013-12-06 One thing that I tried and that showed improvement is to pass an external transaction to SqlBulkCopy constructor. But I still do not understand why not having transaction would fail.
EDIT: 2013-12-24 The stack trace for ArgumentOutOfRange exception
[ L'index était hors limites. Il ne doit pas être négatif et doit être inférieur à la taille de la collection.
Nom du paramètre : index at à System.Collections.ArrayList.get_Item(Int32 index)
à System.Data.SqlClient.BulkCopySimpleResultSet.get_Item(Int32 idx)
à System.Data.SqlClient.SqlBulkCopy.AnalyzeTargetAndCreateUpdateBulkCommand(BulkCopySimpleRes ultSet internalResults)
à System.Data.SqlClient.SqlBulkCopy.WriteToServerInternal()
à System.Data.SqlClient.SqlBulkCopy.WriteRowSourceToServer(Int32 columnCount)
à System.Data.SqlClient.SqlBulkCopy.WriteToServer(IDataReader reader)
Translates to
Index was out of range. It must not be negative and must be less than the size of the collection.
Also, having transaction does not change it. I thought it did but I do see errors when my aplication starts updating the files.