Skip to content

Commit 1b8d1dc

Browse files
authored
Add SqlState and IsTransient to DbException (#39157)
Closes #35601 Closes #34817
1 parent 3c4cfb4 commit 1b8d1dc

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/libraries/System.Data.Common/ref/System.Data.Common.cs

+2
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,8 @@ protected DbException(System.Runtime.Serialization.SerializationInfo info, Syste
22062206
protected DbException(string? message) { }
22072207
protected DbException(string? message, System.Exception? innerException) { }
22082208
protected DbException(string? message, int errorCode) { }
2209+
public virtual bool IsTransient { get { throw null; } }
2210+
public virtual string? SqlState { get { throw null; } }
22092211
}
22102212
public static partial class DbMetaDataCollectionNames
22112213
{

src/libraries/System.Data.Common/src/System/Data/Common/DbException.cs

+23
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,28 @@ protected DbException(string? message, int errorCode) : base(message, errorCode)
2020
protected DbException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context)
2121
{
2222
}
23+
24+
/// <summary>
25+
/// Indicates whether the error represented by this <see cref="DbException" /> could be a transient error, i.e. if retrying the triggering
26+
/// operation may succeed without any other change. Examples of transient errors include failure to acquire a database lock, networking
27+
/// issues. This allows automatic retry execution strategies to be developed without knowledge of specific database error codes.
28+
/// </summary>
29+
public virtual bool IsTransient => false;
30+
31+
/// <summary>
32+
/// <para>
33+
/// For database providers which support it, contains a standard SQL 5-character return code indicating the success or failure of
34+
/// the database operation. The first 2 characters represent the <strong>class</strong> of the return code (e.g. error, success),
35+
/// while the last 3 characters represent the <strong>subclass</strong>, allowing detection of error scenarios in a
36+
/// database-portable way.
37+
/// </para>
38+
/// <para>
39+
/// For database providers which don't support it, or for inapplicable error scenarios, contains <see langword="null" />.
40+
/// </para>
41+
/// </summary>
42+
/// <returns>
43+
/// A standard SQL 5-character return code, or <see langword="null" />.
44+
/// </returns>
45+
public virtual string? SqlState => null;
2346
}
2447
}

src/libraries/System.Data.Common/tests/System/Data/Common/DbExceptionTests.cs

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#nullable enable
5+
46
using Xunit;
57

68
namespace System.Data.Common.Tests
@@ -32,6 +34,13 @@ public void Ctor_ArgumentsRoundtrip()
3234
Assert.Equal(4060, e.ErrorCode);
3335
}
3436

37+
[Fact]
38+
public void IsTransient_is_false_by_default()
39+
=> Assert.False(new CustomDbException().IsTransient);
40+
41+
[Fact]
42+
public void SqlState_is_null_by_default()
43+
=> Assert.Null(new CustomDbException().SqlState);
3544

3645
private class CustomDbException : DbException
3746
{

0 commit comments

Comments
 (0)