1
1
using System ;
2
- using System . Collections ;
3
- using System . Collections . Generic ;
2
+ using System . Collections . Concurrent ;
4
3
using System . Data ;
4
+ using System . Linq ;
5
5
using NHibernate . Connection ;
6
6
7
7
namespace NHibernate . Test
@@ -12,70 +12,49 @@ namespace NHibernate.Test
12
12
/// </summary>
13
13
public class DebugConnectionProvider : DriverConnectionProvider
14
14
{
15
- private ISet < IDbConnection > connections = new HashSet < IDbConnection > ( ) ;
15
+ private ConcurrentDictionary < IDbConnection , byte > connections = new ConcurrentDictionary < IDbConnection , byte > ( ) ;
16
16
17
17
public override IDbConnection GetConnection ( )
18
18
{
19
19
try
20
20
{
21
21
IDbConnection connection = base . GetConnection ( ) ;
22
- connections . Add ( connection ) ;
22
+ connections . TryAdd ( connection , 0 ) ;
23
23
return connection ;
24
24
}
25
25
catch ( Exception e )
26
26
{
27
27
throw new HibernateException ( "Could not open connection to: " + ConnectionString , e ) ;
28
28
}
29
-
30
29
}
31
30
32
31
public override void CloseConnection ( IDbConnection conn )
33
32
{
34
33
base . CloseConnection ( conn ) ;
35
- connections . Remove ( conn ) ;
34
+ byte _ ;
35
+ connections . TryRemove ( conn , out _ ) ;
36
36
}
37
37
38
38
public bool HasOpenConnections
39
39
{
40
40
get
41
41
{
42
- // check to see if all connections that were at one point opened
43
- // have been closed through the CloseConnection
44
- // method
45
- if ( connections . Count == 0 )
46
- {
47
- // there are no connections, either none were opened or
48
- // all of the closings went through CloseConnection.
49
- return false ;
50
- }
51
- else
52
- {
53
- // Disposing of an ISession does not call CloseConnection (should it???)
54
- // so a Diposed of ISession will leave an IDbConnection in the list but
55
- // the IDbConnection will be closed (atleast with MsSql it works this way).
56
- foreach ( IDbConnection conn in connections )
57
- {
58
- if ( conn . State != ConnectionState . Closed )
59
- {
60
- return true ;
61
- }
62
- }
63
-
64
- // all of the connections have been Disposed and were closed that way
65
- // or they were Closed through the CloseConnection method.
66
- return false ;
67
- }
42
+ // Disposing of an ISession does not call CloseConnection (should it???)
43
+ // so a Diposed of ISession will leave an IDbConnection in the list but
44
+ // the IDbConnection will be closed (atleast with MsSql it works this way).
45
+ return connections . Keys . Any ( conn => conn . State != ConnectionState . Closed ) ;
68
46
}
69
47
}
70
48
71
49
public void CloseAllConnections ( )
72
50
{
73
51
while ( connections . Count != 0 )
74
52
{
75
- IEnumerator en = connections . GetEnumerator ( ) ;
76
- en . MoveNext ( ) ;
77
- CloseConnection ( en . Current as IDbConnection ) ;
53
+ foreach ( var conn in connections . Keys . ToArray ( ) )
54
+ {
55
+ CloseConnection ( conn ) ;
56
+ }
78
57
}
79
58
}
80
59
}
81
- }
60
+ }
0 commit comments