2
2
3
3
public delegate void OnAdoNetSqlConnectionOpening ( IAdoNetSqlConnectionString connectionString , IDbConnection connection ) ;
4
4
public 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 ) ;
6
6
7
7
public delegate void OnAdoNetSqlConnectionClosing ( DatabaseConnection connection ) ;
8
8
public delegate void OnAdoNetSqlConnectionClosed ( DatabaseConnection connection ) ;
@@ -14,17 +14,17 @@ public class AdoNetSqlConnectionManager
14
14
15
15
private readonly Dictionary < string , DatabaseConnection > _connections = [ ] ;
16
16
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 )
18
18
{
19
19
return GetConnection < DisposableDatabaseConnection > ( connectionString , maxRetryCount , retryDelayMilliseconds , onOpening , onOpened , onError ) ;
20
20
}
21
21
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 )
23
23
{
24
24
return GetConnection < DatabaseConnection > ( connectionString , maxRetryCount , retryDelayMilliseconds , onOpening , onOpened , onError ) ;
25
25
}
26
26
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 )
28
28
where T : DatabaseConnection , new ( )
29
29
{
30
30
var key = connectionString . Name ;
@@ -47,7 +47,7 @@ public T GetConnection<T>(IAdoNetSqlConnectionString connectionString, int maxRe
47
47
key += "-" ;
48
48
}
49
49
50
- List < Exception > exceptions = null ;
50
+ List < Exception > ? exceptions = null ;
51
51
52
52
for ( var retry = 0 ; retry <= maxRetryCount ; retry ++ )
53
53
{
@@ -61,7 +61,7 @@ public T GetConnection<T>(IAdoNetSqlConnectionString connectionString, int maxRe
61
61
62
62
try
63
63
{
64
- IDbConnection conn = null ;
64
+ IDbConnection ? conn = null ;
65
65
66
66
var connectionType = Type . GetType ( connectionString . ProviderName ) ;
67
67
if ( connectionType != null )
@@ -80,9 +80,12 @@ public T GetConnection<T>(IAdoNetSqlConnectionString connectionString, int maxRe
80
80
}
81
81
catch ( Exception ex )
82
82
{
83
- onError ? . Invoke ( connectionString , conn , retry , ex ) ;
83
+ onError ? . Invoke ( connectionString , null , retry , ex ) ;
84
84
throw ;
85
85
}
86
+
87
+ if ( conn == null )
88
+ return null ;
86
89
}
87
90
88
91
conn . ConnectionString = connectionString . ConnectionString ;
@@ -132,15 +135,15 @@ public T GetConnection<T>(IAdoNetSqlConnectionString connectionString, int maxRe
132
135
return null ;
133
136
}
134
137
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 )
136
139
{
137
- Exception lastException = null ;
140
+ Exception ? lastException = null ;
138
141
139
142
for ( var retry = 0 ; retry <= maxRetryCount ; retry ++ )
140
143
{
141
144
try
142
145
{
143
- IDbConnection conn = null ;
146
+ IDbConnection ? conn = null ;
144
147
145
148
var connectionType = Type . GetType ( connectionString . ProviderName ) ;
146
149
if ( connectionType != null )
@@ -162,6 +165,9 @@ public DatabaseConnection GetNewConnection(IAdoNetSqlConnectionString connection
162
165
onError ? . Invoke ( connectionString , conn , retry , ex ) ;
163
166
throw ;
164
167
}
168
+
169
+ if ( conn == null )
170
+ return null ;
165
171
}
166
172
167
173
conn . ConnectionString = connectionString . ConnectionString ;
@@ -203,7 +209,7 @@ public DatabaseConnection GetNewConnection(IAdoNetSqlConnectionString connection
203
209
return null ;
204
210
}
205
211
206
- private string GetTransactionIdentifierString ( Transaction transaction )
212
+ private string ? GetTransactionIdentifierString ( Transaction ? transaction )
207
213
{
208
214
if ( transaction == null )
209
215
return null ;
@@ -241,11 +247,8 @@ public void ConnectionFailed(DatabaseConnection connection)
241
247
}
242
248
}
243
249
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 )
245
251
{
246
- if ( connection == null )
247
- return ;
248
-
249
252
lock ( _connections )
250
253
{
251
254
connection . ReferenceCount -- ;
@@ -275,7 +278,7 @@ public void ReleaseConnection(DatabaseConnection connection, OnAdoNetSqlConnecti
275
278
276
279
public void TestConnection ( IAdoNetSqlConnectionString connectionString )
277
280
{
278
- IDbConnection conn = null ;
281
+ IDbConnection ? conn = null ;
279
282
280
283
var connectionType = Type . GetType ( connectionString . ProviderName ) ;
281
284
if ( connectionType != null )
@@ -285,10 +288,13 @@ public void TestConnection(IAdoNetSqlConnectionString connectionString)
285
288
286
289
conn ??= DbProviderFactories . GetFactory ( connectionString . ProviderName ) . CreateConnection ( ) ;
287
290
288
- conn . ConnectionString = connectionString . ConnectionString ;
289
- conn . Open ( ) ;
291
+ if ( conn != null )
292
+ {
293
+ conn . ConnectionString = connectionString . ConnectionString ;
294
+ conn . Open ( ) ;
290
295
291
- conn . Close ( ) ;
292
- conn . Dispose ( ) ;
296
+ conn . Close ( ) ;
297
+ conn . Dispose ( ) ;
298
+ }
293
299
}
294
- }
300
+ }
0 commit comments