Skip to content

Commit 959f23f

Browse files
Catch SqlException for existing object error (#456)
* Catch SqlException for existing object error * Update src/TriggerBinding/SqlTriggerListener.cs Co-authored-by: Charles Gagnon <chgagnon@microsoft.com> Co-authored-by: Charles Gagnon <chgagnon@microsoft.com>
1 parent 7db9f60 commit 959f23f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/Telemetry/Telemetry.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ public enum TelemetryErrorName
408408
{
409409
ConsumeChangesLoop,
410410
Convert,
411+
CreateSchema,
411412
FlushAsync,
412413
GetCaseSensitivity,
413414
GetChanges,

src/TriggerBinding/SqlTriggerListener.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,29 @@ IF SCHEMA_ID(N'{SchemaName}') IS NULL
354354
using (var createSchemaCommand = new SqlCommand(createSchemaQuery, connection, transaction))
355355
{
356356
var stopwatch = Stopwatch.StartNew();
357-
await createSchemaCommand.ExecuteNonQueryAsync(cancellationToken);
357+
358+
try
359+
{
360+
await createSchemaCommand.ExecuteNonQueryAsync(cancellationToken);
361+
}
362+
catch (Exception ex)
363+
{
364+
TelemetryInstance.TrackException(TelemetryErrorName.CreateSchema, ex, this._telemetryProps);
365+
var sqlEx = ex as SqlException;
366+
if (sqlEx?.Number == 2714)
367+
{
368+
// Error 2714 is for an object of that name already existing in the database. This generally shouldn't happen
369+
// since we check for its existence in the statement but occasionally a race condition can make it so
370+
// that multiple instances will try and create the schema at once. In that case we can just ignore the
371+
// error since all we care about is that the schema exists at all.
372+
this._logger.LogWarning($"Failed to create schema '{SchemaName}'. Exception message: {ex.Message} This is informational only, function startup will continue as normal.");
373+
}
374+
else
375+
{
376+
throw;
377+
}
378+
}
379+
358380
long durationMs = stopwatch.ElapsedMilliseconds;
359381
this._logger.LogDebugWithThreadId($"END CreateSchema Duration={durationMs}ms");
360382
return durationMs;

0 commit comments

Comments
 (0)