Skip to content

Commit

Permalink
Add SqlState and IsTransient to DbException (#39157)
Browse files Browse the repository at this point in the history
Closes #35601
Closes #34817
  • Loading branch information
roji committed Jul 12, 2020
1 parent 3c4cfb4 commit 1b8d1dc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/libraries/System.Data.Common/ref/System.Data.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,8 @@ protected DbException(System.Runtime.Serialization.SerializationInfo info, Syste
protected DbException(string? message) { }
protected DbException(string? message, System.Exception? innerException) { }
protected DbException(string? message, int errorCode) { }
public virtual bool IsTransient { get { throw null; } }
public virtual string? SqlState { get { throw null; } }
}
public static partial class DbMetaDataCollectionNames
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,28 @@ protected DbException(string? message, int errorCode) : base(message, errorCode)
protected DbException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
{
}

/// <summary>
/// Indicates whether the error represented by this <see cref="DbException" /> could be a transient error, i.e. if retrying the triggering
/// operation may succeed without any other change. Examples of transient errors include failure to acquire a database lock, networking
/// issues. This allows automatic retry execution strategies to be developed without knowledge of specific database error codes.
/// </summary>
public virtual bool IsTransient => false;

/// <summary>
/// <para>
/// For database providers which support it, contains a standard SQL 5-character return code indicating the success or failure of
/// the database operation. The first 2 characters represent the <strong>class</strong> of the return code (e.g. error, success),
/// while the last 3 characters represent the <strong>subclass</strong>, allowing detection of error scenarios in a
/// database-portable way.
/// </para>
/// <para>
/// For database providers which don't support it, or for inapplicable error scenarios, contains <see langword="null" />.
/// </para>
/// </summary>
/// <returns>
/// A standard SQL 5-character return code, or <see langword="null" />.
/// </returns>
public virtual string? SqlState => null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using Xunit;

namespace System.Data.Common.Tests
Expand Down Expand Up @@ -32,6 +34,13 @@ public void Ctor_ArgumentsRoundtrip()
Assert.Equal(4060, e.ErrorCode);
}

[Fact]
public void IsTransient_is_false_by_default()
=> Assert.False(new CustomDbException().IsTransient);

[Fact]
public void SqlState_is_null_by_default()
=> Assert.Null(new CustomDbException().SqlState);

private class CustomDbException : DbException
{
Expand Down

0 comments on commit 1b8d1dc

Please sign in to comment.