forked from nhibernate/nhibernate-core-testcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugConnectionProvider.cs
81 lines (74 loc) · 2.03 KB
/
DebugConnectionProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections;
using System.Data;
using Iesi.Collections;
using NHibernate.Connection;
namespace NHibernate.Test
{
/// <summary>
/// This connection provider keeps a list of all open connections,
/// it is used when testing to check that tests clean up after themselves.
/// </summary>
public class DebugConnectionProvider : DriverConnectionProvider
{
private ISet connections = new ListSet();
public override IDbConnection GetConnection()
{
try
{
IDbConnection connection = base.GetConnection();
connections.Add(connection);
return connection;
}
catch (Exception e)
{
throw new HibernateException("Could not open connection to: " + ConnectionString, e);
}
}
public override void CloseConnection(IDbConnection conn)
{
base.CloseConnection(conn);
connections.Remove(conn);
}
public bool HasOpenConnections
{
get
{
// check to see if all connections that were at one point opened
// have been closed through the CloseConnection
// method
if (connections.IsEmpty)
{
// there are no connections, either none were opened or
// all of the closings went through CloseConnection.
return false;
}
else
{
// Disposing of an ISession does not call CloseConnection (should it???)
// so a Diposed of ISession will leave an IDbConnection in the list but
// the IDbConnection will be closed (atleast with MsSql it works this way).
foreach (IDbConnection conn in connections)
{
if (conn.State != ConnectionState.Closed)
{
return true;
}
}
// all of the connections have been Disposed and were closed that way
// or they were Closed through the CloseConnection method.
return false;
}
}
}
public void CloseAllConnections()
{
while (!connections.IsEmpty)
{
IEnumerator en = connections.GetEnumerator();
en.MoveNext();
CloseConnection(en.Current as IDbConnection);
}
}
}
}