private async Task UseStoredProcedure(IList <EventEntry> collection) { var token = this.cancellationTokenSource.Token; await this.retryPolicy.ExecuteAsync( async() => { using (var conn = new SqlConnection(this.connectionString)) { await conn.SuppressTransactionOpenAsync(token).ConfigureAwait(false); using (var reader = new EventEntryDataReader(collection, this.instanceName)) using (var cmd = new SqlCommand("dbo.WriteTraces", conn)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@InsertTraces", SqlDbType.Structured)); cmd.Parameters[0].Value = reader; cmd.Parameters[0].TypeName = "dbo.TracesType"; return(await cmd.ExecuteNonQueryAsync(token).ConfigureAwait(false)); } } }, token).ConfigureAwait(false); }
private async Task UseSqlBulkCopy(IList <EventEntry> collection) { int initialCount = collection.Count; for (int retries = 0; retries < 3; retries++) { using (var reader = new EventEntryDataReader(collection, this.instanceName)) { try { await this.TrySqlBulkCopy(reader).ConfigureAwait(false); return; } catch (InvalidOperationException ex) { //// if all events were published throw if (reader.RecordsAffected == collection.Count) { throw; } int affectedRow = reader.RecordsAffected - 1; SemanticLoggingEventSource.Log.DatabaseSinkPublishEventsFailedAndDiscardSingleEntry(ex.Message, affectedRow); ////retry after removing the offending record collection.RemoveAt(affectedRow); } } } ////If still pending events after all retries, discard batch and log. if (initialCount != collection.Count) { SemanticLoggingEventSource.Log.DatabaseSinkPublishEventsFailedAndDiscardsEntries(collection.Count); } }
private async Task UseSqlBulkCopy(IList <EventEntry> collection) { int initialCount = collection.Count; for (int retries = 0; retries < 3; retries++) { using (var reader = new EventEntryDataReader(collection, instanceName)) { try { await TrySqlBulkCopy(reader).ConfigureAwait(false); return; } catch (InvalidOperationException ex) { // if all events were published throw if (reader.RecordsAffected == collection.Count) { throw; } int affectedRow = reader.RecordsAffected - 1; SemanticLoggingEventSource.Log.CustomSinkUnhandledFault(ex.Message); // retry after removing the offending record collection.RemoveAt(affectedRow); } } } // If still pending events after all retries, discard batch and log. if (initialCount != collection.Count) { SemanticLoggingEventSource.Log.CustomSinkUnhandledFault(I18NHelper.FormatInvariant("{0} EventData entries are discarded due to too many failed retries", collection.Count)); } }