@@ -28,6 +28,9 @@ public class RetryLogicConfigHelper
28
28
29
29
private const string SqlRetryLogicTypeName = "Microsoft.Data.SqlClient.SqlRetryLogic" ;
30
30
31
+ private const string CreateExceptionMethodName = "CreateException" ;
32
+ private const string AddMethodName = "Add" ;
33
+
31
34
public const string DefaultTransientErrors = "1204, 1205, 1222, 49918, 49919, 49920, 4060, 4221, 40143, 40613, 40501, 40540, 40197, 42108, 42109, 10929, 10928, 10060, 10054, 10053, 997, 233, 64, 20, 0, -2, 207, 102, 2812" ;
32
35
33
36
private static readonly Random s_random = new Random ( ) ;
@@ -36,13 +39,23 @@ public class RetryLogicConfigHelper
36
39
private static readonly Type s_appContextSwitchManagerType = s_sqlClientAssembly . GetType ( AppContextSwitchManagerTypeName ) ;
37
40
private static readonly Type s_sqlretrylogicType = s_sqlClientAssembly . GetType ( SqlRetryLogicTypeName ) ;
38
41
private static readonly Type s_configurationLoaderType = s_sqlClientAssembly . GetType ( ConfigurationLoaderTypeName ) ;
42
+ private static readonly Type s_sqlErrorType = typeof ( SqlError ) ;
43
+ private static readonly Type s_sqlErrorCollectionType = typeof ( SqlErrorCollection ) ;
39
44
private static readonly Type [ ] s_cfgLoaderParamsType = new Type [ ]
40
45
{
41
46
s_sqlClientAssembly . GetType ( InterfaceCnnCfgTypeName ) ,
42
47
s_sqlClientAssembly . GetType ( InterfaceCmdCfgTypeName ) ,
43
48
typeof ( string ) , typeof ( string )
44
49
} ;
50
+ private static readonly Type [ ] s_sqlErrorParamsType = new Type [ ]
51
+ {
52
+ typeof ( int ) , typeof ( byte ) , typeof ( byte ) ,
53
+ typeof ( string ) , typeof ( string ) , typeof ( string ) ,
54
+ typeof ( int ) , typeof ( Exception )
55
+ } ;
45
56
private static readonly ConstructorInfo s_loaderCtorInfo = s_configurationLoaderType . GetConstructor ( s_cfgLoaderParamsType ) ;
57
+ private static readonly ConstructorInfo s_sqlErrorCtorInfo = s_sqlErrorType . GetConstructor ( BindingFlags . Instance | BindingFlags . NonPublic , null , s_sqlErrorParamsType , null ) ;
58
+ private static readonly ConstructorInfo s_sqlErrorCollectionCtorInfo = s_sqlErrorCollectionType . GetConstructor ( BindingFlags . Instance | BindingFlags . NonPublic , null , Type . EmptyTypes , null ) ;
46
59
47
60
public static object CreateLoader ( RetryLogicConfigs cnnConfig , RetryLogicConfigs cmdConfig )
48
61
{
@@ -164,6 +177,22 @@ public static RetryLogicConfigs CreateRandomConfig(string method, string authori
164
177
} ;
165
178
}
166
179
180
+ public static SqlException CreateSqlException ( int errorNumber )
181
+ {
182
+ MethodInfo addSqlErrorMethod = typeof ( SqlErrorCollection ) . GetMethod ( AddMethodName , BindingFlags . Instance | BindingFlags . NonPublic ) ;
183
+ MethodInfo createExceptionMethod = typeof ( SqlException ) . GetMethod ( CreateExceptionMethodName , BindingFlags . Static | BindingFlags . NonPublic ,
184
+ null , new Type [ ] { typeof ( SqlErrorCollection ) , typeof ( string ) } , null ) ;
185
+
186
+ SqlError sqlError = s_sqlErrorCtorInfo . Invoke ( new object [ ] { errorNumber , ( byte ) 0 , ( byte ) 0 , string . Empty , string . Empty , string . Empty , 0 , null } ) as SqlError ;
187
+ SqlErrorCollection sqlErrorCollection = s_sqlErrorCollectionCtorInfo . Invoke ( new object [ 0 ] { } ) as SqlErrorCollection ;
188
+
189
+ addSqlErrorMethod . Invoke ( sqlErrorCollection , new object [ ] { sqlError } ) ;
190
+
191
+ SqlException sqlException = createExceptionMethod . Invoke ( null , new object [ ] { sqlErrorCollection , string . Empty } ) as SqlException ;
192
+
193
+ return sqlException ;
194
+ }
195
+
167
196
private static TimeSpan GenerateTimeSpan ( TimeSpan start , TimeSpan end )
168
197
{
169
198
int max = ( int ) ( end - start ) . TotalSeconds ;
0 commit comments