0

I have an MVC app/api that accepts a JSON request from an AngularJs application. The request contains: a Lead property, a VisitorID (Guid), and a collection of schools. For the collection of schools I generate tasks that will submit each school to it's appropriate destination and return a SubmissionEvent object. I call Task.WhenAll() on the collection of tasks to run them in parallel, and when it finishes I flatten the collection of SubmissionEvents into an array. Then I BulkInsert them into my database. Easy peasy. Everything works.

The way the front-end angular app works, no more than 3 schools should be submitted at a time, and therefore BulkInsert should not be inserting more than 3 schools at a time.

Randomly, and only in production environment, BulkInsert will sometimes insert literally thousands (20k at one point) of records, all with the same info. The primary key auto-increments on insert, as well as the timestamp.

                using (var scope = new TransactionScope())
                {
                    // BulkInsert submissionEvents.
                    var options = new BulkInsertOptions { EnableStreaming = true, };

                    if (submissionEvents != null && submissionEvents.Count() > 0)
                        db.BulkInsert(submissionEvents, options);

                    scope.Complete();
                }

How is this happening and how do I fix it?

Bryan
  • 101
  • 10
  • I'd hazard a guess that randomly the `submissionEvents` object contains 20k+ extra records but who knows. Add some logging so you can see where it's happening. – DavidG Aug 19 '16 at 00:14
  • I've added additional logging to notify me if the generated SubmissionEvents collection is greater than 3. The way the app works, a user sees anywhere from 0-20 schools and can pick up to 3. The interface doesn't allow more than that. Once submitted I do something like `schools.Select(s => new SubmissionEvent(s)).ToArray();` Then call `db.BulkInsert`. At this point there are now 20k duplicate records. Often when I check my logs when this anomaly occurs, there was only 1 school shown to the user (and therefore only 1 to be submitted). – Bryan Aug 19 '16 at 00:38
  • I suspect there is an issue with the bulk insert library you are using then? – DavidG Aug 19 '16 at 09:41

0 Answers0