Skip to content

Commit 292c1c2

Browse files
committed
Proper close connections
1 parent 0f3882b commit 292c1c2

File tree

9 files changed

+83
-56
lines changed

9 files changed

+83
-56
lines changed

src/NHibernate.Test/ConnectionTest/AggressiveReleaseTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public void SuppliedConnection()
192192
session.Flush();
193193

194194
Release(session);
195-
originalConnection.Close();
195+
sessions.ConnectionProvider.CloseConnection(originalConnection);
196196
Done();
197197
}
198198

@@ -245,4 +245,4 @@ public void ConnectionMaintanenceDuringFlush()
245245
Done();
246246
}
247247
}
248-
}
248+
}

src/NHibernate.Test/DialectTest/DialectFixture.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,24 @@ public void CurrentTimestampSelection()
159159
sessions.ConnectionProvider.Configure(conf.Properties);
160160
IDriver driver = sessions.ConnectionProvider.Driver;
161161

162-
using (IDbConnection connection = sessions.ConnectionProvider.GetConnection())
162+
var connection = sessions.ConnectionProvider.GetConnection();
163+
try
163164
{
164-
IDbCommand statement = driver.GenerateCommand(CommandType.Text, new SqlString(dialect.CurrentTimestampSelectString),
165-
new SqlType[0]);
165+
IDbCommand statement = driver.GenerateCommand(
166+
CommandType.Text,
167+
new SqlString(dialect.CurrentTimestampSelectString),
168+
new SqlType[0]);
166169
statement.Connection = connection;
167170
using (IDataReader reader = statement.ExecuteReader())
168171
{
169172
Assert.That(reader.Read(), "should return one record");
170173
Assert.That(reader[0], Is.InstanceOf<DateTime>());
171174
}
172175
}
176+
finally
177+
{
178+
sessions.ConnectionProvider.CloseConnection(connection);
179+
}
173180
}
174-
175181
}
176-
}
182+
}

src/NHibernate.Test/DriverTest/FirebirdClientDriverFixture.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@ public class FirebirdClientDriverFixture
1616
public void ConnectionPooling_OpenThenCloseThenOpenAnotherOne_OnlyOneConnectionIsPooled()
1717
{
1818
MakeDriver();
19-
var connection1 = MakeConnection();
20-
var connection2 = MakeConnection();
19+
using (var connection1 = MakeConnection())
20+
using (var connection2 = MakeConnection())
21+
{
22+
connection1.Open();
23+
VerifyCountOfEstablishedConnectionsIs(1);
2124

22-
//open first connection
23-
connection1.Open();
24-
VerifyCountOfEstablishedConnectionsIs(1);
25+
//return it to the pool
26+
connection1.Close();
27+
VerifyCountOfEstablishedConnectionsIs(1);
2528

26-
//return it to the pool
27-
connection1.Close();
28-
VerifyCountOfEstablishedConnectionsIs(1);
29+
//open the second connection
30+
connection2.Open();
31+
VerifyCountOfEstablishedConnectionsIs(1);
2932

30-
//open the second connection
31-
connection2.Open();
32-
VerifyCountOfEstablishedConnectionsIs(1);
33+
//return it to the pool
34+
connection2.Close();
3335

34-
//return it to the pool
35-
connection2.Close();
36-
VerifyCountOfEstablishedConnectionsIs(1);
36+
VerifyCountOfEstablishedConnectionsIs(1);
37+
}
3738
}
3839

3940
[Test]

src/NHibernate.Test/Legacy/FooBarTest.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4978,20 +4978,27 @@ public void AutoFlushCollections()
49784978
public void UserProvidedConnection()
49794979
{
49804980
IConnectionProvider prov = ConnectionProviderFactory.NewConnectionProvider(cfg.Properties);
4981-
ISession s = sessions.OpenSession(prov.GetConnection());
4982-
ITransaction tx = s.BeginTransaction();
4983-
s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
4984-
tx.Commit();
4981+
var conn = prov.GetConnection();
4982+
try
4983+
{
4984+
ISession s = sessions.OpenSession(conn);
4985+
ITransaction tx = s.BeginTransaction();
4986+
s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
4987+
tx.Commit();
49854988

4986-
IDbConnection c = s.Disconnect();
4987-
Assert.IsNotNull(c);
4989+
IDbConnection c = s.Disconnect();
4990+
Assert.IsNotNull(c);
49884991

4989-
s.Reconnect(c);
4990-
tx = s.BeginTransaction();
4991-
s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
4992-
tx.Commit();
4993-
Assert.AreSame(c, s.Close());
4994-
c.Close();
4992+
s.Reconnect(c);
4993+
tx = s.BeginTransaction();
4994+
s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
4995+
tx.Commit();
4996+
Assert.AreSame(c, s.Close());
4997+
}
4998+
finally
4999+
{
5000+
prov.CloseConnection(conn);
5001+
}
49955002
}
49965003

49975004
[Test]

src/NHibernate.Test/Linq/LinqReadonlyTestsContext.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ private void ExecuteScriptFile(Configuration configuration, string scripFileName
7171
var file = new FileInfo(scripFileName);
7272
string script = file.OpenText().ReadToEnd().Replace("GO", "");
7373
var connectionProvider = ConnectionProviderFactory.NewConnectionProvider(configuration.GetDerivedProperties());
74-
using (var conn = connectionProvider.GetConnection())
74+
var conn = connectionProvider.GetConnection();
75+
try
7576
{
7677
if (conn.State == ConnectionState.Closed)
7778
{
@@ -83,6 +84,10 @@ private void ExecuteScriptFile(Configuration configuration, string scripFileName
8384
command.ExecuteNonQuery();
8485
}
8586
}
87+
finally
88+
{
89+
connectionProvider.CloseConnection(conn);
90+
}
8691
}
8792

8893
[TearDown]
@@ -143,4 +148,4 @@ private void CreateTestData(ISessionFactory sessionFactory)
143148
}
144149
}
145150
}
146-
}
151+
}

src/NHibernate.Test/NHSpecificTest/UserTypeFixture.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,32 @@ public void InsertNull()
3939
// manually read from the db
4040
IConnectionProvider provider = ConnectionProviderFactory.NewConnectionProvider(cfg.Properties);
4141
IDbConnection conn = provider.GetConnection();
42-
IDbCommand cmd = conn.CreateCommand();
43-
cmd.Connection = conn;
44-
cmd.CommandText = "select * from usertype";
42+
try
43+
{
44+
IDbCommand cmd = conn.CreateCommand();
45+
cmd.Connection = conn;
46+
cmd.CommandText = "select * from usertype";
4547

46-
IDataReader reader = cmd.ExecuteReader();
48+
IDataReader reader = cmd.ExecuteReader();
4749

48-
while (reader.Read())
50+
while (reader.Read())
51+
{
52+
Assert.AreEqual(5, reader[0]);
53+
Assert.AreEqual(4, reader[1]);
54+
Assert.AreEqual(DBNull.Value, reader[2]);
55+
break;
56+
}
57+
}
58+
finally
4959
{
50-
Assert.AreEqual(5, reader[0]);
51-
Assert.AreEqual(4, reader[1]);
52-
Assert.AreEqual(DBNull.Value, reader[2]);
53-
break;
60+
provider.CloseConnection(conn);
5461
}
5562

56-
conn.Close();
57-
5863
using (ISession s = OpenSession())
5964
{
6065
s.Delete("from ClassWithNullColumns");
6166
s.Flush();
6267
}
6368
}
6469
}
65-
}
70+
}

src/NHibernate.Test/SystemTransactions/TransactionNotificationFixture.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,12 @@ public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doComm
161161

162162
using (var tx = new TransactionScope())
163163
{
164-
IDbConnection ownConnection1 = sessions.ConnectionProvider.GetConnection();
165-
164+
var ownConnection = sessions.ConnectionProvider.GetConnection();
166165
try
167166
{
168167
try
169168
{
170-
s1 = sessions.OpenSession(ownConnection1, interceptor);
169+
s1 = sessions.OpenSession(ownConnection, interceptor);
171170

172171
s1.CreateCriteria<object>().List();
173172
}
@@ -182,7 +181,7 @@ public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doComm
182181
}
183182
finally
184183
{
185-
sessions.ConnectionProvider.CloseConnection(ownConnection1);
184+
sessions.ConnectionProvider.CloseConnection(ownConnection);
186185
}
187186
}
188187

@@ -191,6 +190,5 @@ public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doComm
191190

192191
Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
193192
}
194-
195193
}
196-
}
194+
}

src/NHibernate.Test/TestCase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public int ExecuteStatement(string sql)
275275

276276
using (IConnectionProvider prov = ConnectionProviderFactory.NewConnectionProvider(cfg.Properties))
277277
{
278-
IDbConnection conn = prov.GetConnection();
278+
var conn = prov.GetConnection();
279279

280280
try
281281
{

src/NHibernate.Test/TransactionTest/TransactionNotificationFixture.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ public void ShouldNotifyAfterTransactionWithOwnConnection(bool usePrematureClose
9696
var interceptor = new RecordingInterceptor();
9797
ISession s;
9898

99-
using (IDbConnection ownConnection = sessions.ConnectionProvider.GetConnection())
99+
var ownConnection = sessions.ConnectionProvider.GetConnection();
100+
try
100101
{
101102
using (s = sessions.OpenSession(ownConnection, interceptor))
102103
using (s.BeginTransaction())
@@ -108,9 +109,13 @@ public void ShouldNotifyAfterTransactionWithOwnConnection(bool usePrematureClose
108109
s.Close();
109110
}
110111
}
112+
finally
113+
{
114+
sessions.ConnectionProvider.CloseConnection(ownConnection);
115+
}
111116

112117
Assert.That(s.IsOpen, Is.False);
113118
Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
114119
}
115120
}
116-
}
121+
}

0 commit comments

Comments
 (0)