22
33public delegate void OnAdoNetSqlConnectionOpening ( IAdoNetSqlConnectionString connectionString , IDbConnection connection ) ;
44public delegate void OnAdoNetSqlConnectionOpened ( IAdoNetSqlConnectionString connectionString , IDbConnection connection , int retryCount ) ;
5- public delegate void OnAdoNetSqlConnectionOpenError ( IAdoNetSqlConnectionString connectionString , IDbConnection connection , int retryCount , Exception ex ) ;
5+ public delegate void OnAdoNetSqlConnectionOpenError ( IAdoNetSqlConnectionString connectionString , IDbConnection ? connection , int retryCount , Exception ex ) ;
66
77public delegate void OnAdoNetSqlConnectionClosing ( DatabaseConnection connection ) ;
88public delegate void OnAdoNetSqlConnectionClosed ( DatabaseConnection connection ) ;
@@ -14,17 +14,17 @@ public class AdoNetSqlConnectionManager
1414
1515 private readonly Dictionary < string , DatabaseConnection > _connections = [ ] ;
1616
17- public DisposableDatabaseConnection GetDisposableConnection ( IAdoNetSqlConnectionString connectionString , int maxRetryCount = 5 , int retryDelayMilliseconds = 2000 , OnAdoNetSqlConnectionOpening onOpening = null , OnAdoNetSqlConnectionOpened onOpened = null , OnAdoNetSqlConnectionOpenError onError = null )
17+ public DisposableDatabaseConnection ? GetDisposableConnection ( IAdoNetSqlConnectionString connectionString , int maxRetryCount = 5 , int retryDelayMilliseconds = 2000 , OnAdoNetSqlConnectionOpening ? onOpening = null , OnAdoNetSqlConnectionOpened ? onOpened = null , OnAdoNetSqlConnectionOpenError ? onError = null )
1818 {
1919 return GetConnection < DisposableDatabaseConnection > ( connectionString , maxRetryCount , retryDelayMilliseconds , onOpening , onOpened , onError ) ;
2020 }
2121
22- public DatabaseConnection GetConnection ( IAdoNetSqlConnectionString connectionString , int maxRetryCount = 5 , int retryDelayMilliseconds = 2000 , OnAdoNetSqlConnectionOpening onOpening = null , OnAdoNetSqlConnectionOpened onOpened = null , OnAdoNetSqlConnectionOpenError onError = null )
22+ public DatabaseConnection ? GetConnection ( IAdoNetSqlConnectionString connectionString , int maxRetryCount = 5 , int retryDelayMilliseconds = 2000 , OnAdoNetSqlConnectionOpening ? onOpening = null , OnAdoNetSqlConnectionOpened ? onOpened = null , OnAdoNetSqlConnectionOpenError ? onError = null )
2323 {
2424 return GetConnection < DatabaseConnection > ( connectionString , maxRetryCount , retryDelayMilliseconds , onOpening , onOpened , onError ) ;
2525 }
2626
27- public T GetConnection < T > ( IAdoNetSqlConnectionString connectionString , int maxRetryCount = 5 , int retryDelayMilliseconds = 2000 , OnAdoNetSqlConnectionOpening onOpening = null , OnAdoNetSqlConnectionOpened onOpened = null , OnAdoNetSqlConnectionOpenError onError = null )
27+ public T ? GetConnection < T > ( IAdoNetSqlConnectionString connectionString , int maxRetryCount = 5 , int retryDelayMilliseconds = 2000 , OnAdoNetSqlConnectionOpening ? onOpening = null , OnAdoNetSqlConnectionOpened ? onOpened = null , OnAdoNetSqlConnectionOpenError ? onError = null )
2828 where T : DatabaseConnection , new ( )
2929 {
3030 var key = connectionString . Name ;
@@ -47,7 +47,7 @@ public T GetConnection<T>(IAdoNetSqlConnectionString connectionString, int maxRe
4747 key += "-" ;
4848 }
4949
50- List < Exception > exceptions = null ;
50+ List < Exception > ? exceptions = null ;
5151
5252 for ( var retry = 0 ; retry <= maxRetryCount ; retry ++ )
5353 {
@@ -61,7 +61,7 @@ public T GetConnection<T>(IAdoNetSqlConnectionString connectionString, int maxRe
6161
6262 try
6363 {
64- IDbConnection conn = null ;
64+ IDbConnection ? conn = null ;
6565
6666 var connectionType = Type . GetType ( connectionString . ProviderName ) ;
6767 if ( connectionType != null )
@@ -80,9 +80,12 @@ public T GetConnection<T>(IAdoNetSqlConnectionString connectionString, int maxRe
8080 }
8181 catch ( Exception ex )
8282 {
83- onError ? . Invoke ( connectionString , conn , retry , ex ) ;
83+ onError ? . Invoke ( connectionString , null , retry , ex ) ;
8484 throw ;
8585 }
86+
87+ if ( conn == null )
88+ return null ;
8689 }
8790
8891 conn . ConnectionString = connectionString . ConnectionString ;
@@ -132,15 +135,15 @@ public T GetConnection<T>(IAdoNetSqlConnectionString connectionString, int maxRe
132135 return null ;
133136 }
134137
135- public DatabaseConnection GetNewConnection ( IAdoNetSqlConnectionString connectionString , int maxRetryCount = 5 , int retryDelayMilliseconds = 2000 , OnAdoNetSqlConnectionOpening onOpening = null , OnAdoNetSqlConnectionOpened onOpened = null , OnAdoNetSqlConnectionOpenError onError = null )
138+ public DatabaseConnection ? GetNewConnection ( IAdoNetSqlConnectionString connectionString , int maxRetryCount = 5 , int retryDelayMilliseconds = 2000 , OnAdoNetSqlConnectionOpening ? onOpening = null , OnAdoNetSqlConnectionOpened ? onOpened = null , OnAdoNetSqlConnectionOpenError ? onError = null )
136139 {
137- Exception lastException = null ;
140+ Exception ? lastException = null ;
138141
139142 for ( var retry = 0 ; retry <= maxRetryCount ; retry ++ )
140143 {
141144 try
142145 {
143- IDbConnection conn = null ;
146+ IDbConnection ? conn = null ;
144147
145148 var connectionType = Type . GetType ( connectionString . ProviderName ) ;
146149 if ( connectionType != null )
@@ -162,6 +165,9 @@ public DatabaseConnection GetNewConnection(IAdoNetSqlConnectionString connection
162165 onError ? . Invoke ( connectionString , conn , retry , ex ) ;
163166 throw ;
164167 }
168+
169+ if ( conn == null )
170+ return null ;
165171 }
166172
167173 conn . ConnectionString = connectionString . ConnectionString ;
@@ -203,7 +209,7 @@ public DatabaseConnection GetNewConnection(IAdoNetSqlConnectionString connection
203209 return null ;
204210 }
205211
206- private string GetTransactionIdentifierString ( Transaction transaction )
212+ private string ? GetTransactionIdentifierString ( Transaction ? transaction )
207213 {
208214 if ( transaction == null )
209215 return null ;
@@ -241,11 +247,8 @@ public void ConnectionFailed(DatabaseConnection connection)
241247 }
242248 }
243249
244- public void ReleaseConnection ( DatabaseConnection connection , OnAdoNetSqlConnectionClosing onClosing = null , OnAdoNetSqlConnectionClosed onClosed = null , OnAdoNetSqlConnectionCloseError onError = null )
250+ public void ReleaseConnection ( DatabaseConnection connection , OnAdoNetSqlConnectionClosing ? onClosing = null , OnAdoNetSqlConnectionClosed ? onClosed = null , OnAdoNetSqlConnectionCloseError ? onError = null )
245251 {
246- if ( connection == null )
247- return ;
248-
249252 lock ( _connections )
250253 {
251254 connection . ReferenceCount -- ;
@@ -275,7 +278,7 @@ public void ReleaseConnection(DatabaseConnection connection, OnAdoNetSqlConnecti
275278
276279 public void TestConnection ( IAdoNetSqlConnectionString connectionString )
277280 {
278- IDbConnection conn = null ;
281+ IDbConnection ? conn = null ;
279282
280283 var connectionType = Type . GetType ( connectionString . ProviderName ) ;
281284 if ( connectionType != null )
@@ -285,10 +288,13 @@ public void TestConnection(IAdoNetSqlConnectionString connectionString)
285288
286289 conn ??= DbProviderFactories . GetFactory ( connectionString . ProviderName ) . CreateConnection ( ) ;
287290
288- conn . ConnectionString = connectionString . ConnectionString ;
289- conn . Open ( ) ;
291+ if ( conn != null )
292+ {
293+ conn . ConnectionString = connectionString . ConnectionString ;
294+ conn . Open ( ) ;
290295
291- conn . Close ( ) ;
292- conn . Dispose ( ) ;
296+ conn . Close ( ) ;
297+ conn . Dispose ( ) ;
298+ }
293299 }
294- }
300+ }
0 commit comments