11using System ;
22using System . Data . Common ;
3+ using System . Net . Sockets ;
34using System . Threading ;
45using System . Threading . Tasks ;
56using Microsoft . Data . SqlClient ;
@@ -40,7 +41,7 @@ private static DbConnection OpenConnection(Type connectionType)
4041 {
4142 const int maxAttempts = 3 ;
4243 var connectionString = Environment . GetEnvironmentVariable ( "SQLSERVER_CONNECTION_STRING" ) ??
43- @"Server=(localdb) \MSSQLLocalDB;Integrated Security=true;Connection Timeout=60" ;
44+ @"Server=ertpoer \MSSQLLocalDB;Integrated Security=true;Connection Timeout=60" ;
4445
4546 SqlException lastException = null ;
4647
@@ -66,6 +67,15 @@ private static DbConnection OpenConnection(Type connectionType)
6667 Thread . Sleep ( 1000 * attempt ) ;
6768 }
6869 }
70+ catch ( SqlException ex ) when ( ! IsRetryableConnectionError ( ex ) )
71+ {
72+ if ( attempt < maxAttempts )
73+ {
74+ Console . WriteLine ( $ "Fatal SqlException Number: { ex . Number } , State: { ex . State } , Class: { ex . Class } ") ;
75+ Console . WriteLine ( $ "Message: { ex . Message } ") ;
76+ throw ;
77+ }
78+ }
6979 catch ( Exception ex )
7080 {
7181 // Other errors (reflection issues, etc.) should fail the test
@@ -84,17 +94,25 @@ private static DbConnection OpenConnection(Type connectionType)
8494
8595 static bool IsRetryableConnectionError ( SqlException ex )
8696 {
87- return ex . Number == - 1 || // Generic network error
88- ex . Number == - 2 || // Connection timeout
89- ex . Number == 2 || // Network path not found
90- ex . Number == 53 || // SQL Server not found
91- ex . Number == 64 || // Connection broken
92- ex . Number == 258 || // Wait timeout
93- ex . Number == 10053 || // Connection aborted
94- ex . Number == 10054 || // Connection reset
95- ex . Number == 10060 || // Connection timeout
96- ex . Number == 10061 || // Connection refused
97- ex . Number == 11001 ; // DNS failure
97+ // Known reliable error codes
98+ if ( ex . Number == - 1 || // Generic network error
99+ ex . Number == - 2 || // Connection timeout
100+ ex . Number == 53 || // SQL Server not found
101+ ex . Number == 10053 || // Connection aborted
102+ ex . Number == 10054 || // Connection reset
103+ ex . Number == 10060 || // Connection timeout
104+ ex . Number == 11001 ) // DNS failure
105+ {
106+ return true ;
107+ }
108+
109+ // Number=0 with SocketException indicates network issue
110+ if ( ex . Number == 0 && ex . InnerException is SocketException )
111+ {
112+ return true ;
113+ }
114+
115+ return false ;
98116 }
99117 }
100118}
0 commit comments