Skip to content

Commit c8ee044

Browse files
committed
Fix session handling in a bunch of unit tests to close responsibly.
1 parent 28a0a39 commit c8ee044

File tree

4 files changed

+995
-908
lines changed

4 files changed

+995
-908
lines changed

src/NHibernate.Test/CompositeId/ClassWithCompositeIdFixture.cs

Lines changed: 101 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -76,97 +76,99 @@ protected override void OnTearDown()
7676
[Test]
7777
public void TestSimpleCRUD()
7878
{
79-
// insert the new objects
80-
ISession s = OpenSession();
81-
ITransaction t = s.BeginTransaction();
79+
ClassWithCompositeId theClass;
80+
ClassWithCompositeId theSecondClass;
8281

83-
ClassWithCompositeId theClass = new ClassWithCompositeId(id);
84-
theClass.OneProperty = 5;
82+
// insert the new objects
83+
using (ISession s = OpenSession())
84+
using (ITransaction t = s.BeginTransaction())
85+
{
86+
theClass = new ClassWithCompositeId(id);
87+
theClass.OneProperty = 5;
8588

86-
ClassWithCompositeId theSecondClass = new ClassWithCompositeId(secondId);
87-
theSecondClass.OneProperty = 10;
89+
theSecondClass = new ClassWithCompositeId(secondId);
90+
theSecondClass.OneProperty = 10;
8891

89-
s.Save(theClass);
90-
s.Save(theSecondClass);
92+
s.Save(theClass);
93+
s.Save(theSecondClass);
9194

92-
t.Commit();
93-
s.Close();
95+
t.Commit();
96+
}
9497

9598
// verify they were inserted and test the SELECT
99+
ClassWithCompositeId theClass2;
100+
ClassWithCompositeId theSecondClass2;
101+
using (ISession s2 = OpenSession())
102+
using (ITransaction t2 = s2.BeginTransaction())
103+
{
104+
theClass2 = (ClassWithCompositeId) s2.Load(typeof(ClassWithCompositeId), id);
105+
Assert.AreEqual(id, theClass2.Id);
96106

97-
ISession s2 = OpenSession();
98-
ITransaction t2 = s2.BeginTransaction();
99-
100-
ClassWithCompositeId theClass2 = (ClassWithCompositeId) s2.Load(typeof(ClassWithCompositeId), id);
101-
Assert.AreEqual(id, theClass2.Id);
102-
103-
IList results2 = s2.CreateCriteria(typeof(ClassWithCompositeId))
104-
.Add(Expression.Eq("Id", secondId))
105-
.List();
107+
IList results2 = s2.CreateCriteria(typeof(ClassWithCompositeId))
108+
.Add(Expression.Eq("Id", secondId))
109+
.List();
106110

107-
Assert.AreEqual(1, results2.Count);
108-
ClassWithCompositeId theSecondClass2 = (ClassWithCompositeId) results2[0];
111+
Assert.AreEqual(1, results2.Count);
112+
theSecondClass2 = (ClassWithCompositeId) results2[0];
109113

110-
ClassWithCompositeId theClass2Copy = (ClassWithCompositeId) s2.Load(typeof(ClassWithCompositeId), id);
114+
ClassWithCompositeId theClass2Copy = (ClassWithCompositeId) s2.Load(typeof(ClassWithCompositeId), id);
111115

112-
// verify the same results through Criteria & Load were achieved
113-
Assert.AreSame(theClass2, theClass2Copy);
116+
// verify the same results through Criteria & Load were achieved
117+
Assert.AreSame(theClass2, theClass2Copy);
114118

115-
// compare them to the objects created in the first session
116-
Assert.AreEqual(theClass.Id, theClass2.Id);
117-
Assert.AreEqual(theClass.OneProperty, theClass2.OneProperty);
119+
// compare them to the objects created in the first session
120+
Assert.AreEqual(theClass.Id, theClass2.Id);
121+
Assert.AreEqual(theClass.OneProperty, theClass2.OneProperty);
118122

119-
Assert.AreEqual(theSecondClass.Id, theSecondClass2.Id);
120-
Assert.AreEqual(theSecondClass.OneProperty, theSecondClass2.OneProperty);
123+
Assert.AreEqual(theSecondClass.Id, theSecondClass2.Id);
124+
Assert.AreEqual(theSecondClass.OneProperty, theSecondClass2.OneProperty);
121125

122-
// test the update functionallity
123-
theClass2.OneProperty = 6;
124-
theSecondClass2.OneProperty = 11;
126+
// test the update functionallity
127+
theClass2.OneProperty = 6;
128+
theSecondClass2.OneProperty = 11;
125129

126-
s2.Update(theClass2);
127-
s2.Update(theSecondClass2);
130+
s2.Update(theClass2);
131+
s2.Update(theSecondClass2);
128132

129-
t2.Commit();
130-
s2.Close();
133+
t2.Commit();
134+
}
131135

132136
// lets verify the update went through
133-
ISession s3 = OpenSession();
134-
ITransaction t3 = s3.BeginTransaction();
135-
136-
ClassWithCompositeId theClass3 = (ClassWithCompositeId) s3.Load(typeof(ClassWithCompositeId), id);
137-
ClassWithCompositeId theSecondClass3 = (ClassWithCompositeId) s3.Load(typeof(ClassWithCompositeId), secondId);
137+
using (ISession s3 = OpenSession())
138+
using (ITransaction t3 = s3.BeginTransaction())
139+
{
140+
ClassWithCompositeId theClass3 = (ClassWithCompositeId) s3.Load(typeof(ClassWithCompositeId), id);
141+
ClassWithCompositeId theSecondClass3 = (ClassWithCompositeId) s3.Load(typeof(ClassWithCompositeId), secondId);
138142

139-
// check the update properties
140-
Assert.AreEqual(theClass3.OneProperty, theClass2.OneProperty);
141-
Assert.AreEqual(theSecondClass3.OneProperty, theSecondClass2.OneProperty);
143+
// check the update properties
144+
Assert.AreEqual(theClass3.OneProperty, theClass2.OneProperty);
145+
Assert.AreEqual(theSecondClass3.OneProperty, theSecondClass2.OneProperty);
142146

143-
// test the delete method
144-
s3.Delete(theClass3);
145-
s3.Delete(theSecondClass3);
147+
// test the delete method
148+
s3.Delete(theClass3);
149+
s3.Delete(theSecondClass3);
146150

147-
t3.Commit();
148-
s3.Close();
151+
t3.Commit();
152+
}
149153

150154
// lets verify the delete went through
151-
ISession s4 = OpenSession();
152-
153-
try
155+
using (ISession s4 = OpenSession())
154156
{
155-
ClassWithCompositeId theClass4 = (ClassWithCompositeId) s4.Load(typeof(ClassWithCompositeId), id);
157+
try
158+
{
159+
ClassWithCompositeId theClass4 = (ClassWithCompositeId) s4.Load(typeof(ClassWithCompositeId), id);
160+
}
161+
catch (ObjectNotFoundException)
162+
{
163+
// I expect this to be thrown because the object no longer exists...
164+
}
165+
166+
IList results = s4.CreateCriteria(typeof(ClassWithCompositeId))
167+
.Add(Expression.Eq("Id", secondId))
168+
.List();
169+
170+
Assert.AreEqual(0, results.Count);
156171
}
157-
catch (ObjectNotFoundException onfe)
158-
{
159-
// I expect this to be thrown because the object no longer exists...
160-
Assert.IsNotNull(onfe); //getting ride of 'onfe' is never used compile warning
161-
}
162-
163-
IList results = s4.CreateCriteria(typeof(ClassWithCompositeId))
164-
.Add(Expression.Eq("Id", secondId))
165-
.List();
166-
167-
Assert.AreEqual(0, results.Count);
168-
169-
s4.Close();
170172
}
171173

172174
[Test]
@@ -178,53 +180,53 @@ public void Criteria()
178180

179181
// add the new instance to the session so I have something to get results
180182
// back for
181-
ISession s = OpenSession();
182-
s.Save(cId);
183-
s.Flush();
184-
s.Close();
185-
186-
s = OpenSession();
187-
ICriteria c = s.CreateCriteria(typeof(ClassWithCompositeId));
188-
c.Add(Expression.Eq("Id", id));
183+
using (ISession s = OpenSession())
184+
{
185+
s.Save(cId);
186+
s.Flush();
187+
}
189188

190-
// right now just want to see if the Criteria is valid
191-
IList results = c.List();
189+
using (ISession s = OpenSession())
190+
{
191+
ICriteria c = s.CreateCriteria(typeof(ClassWithCompositeId));
192+
c.Add(Expression.Eq("Id", id));
192193

193-
Assert.AreEqual(1, results.Count);
194+
// right now just want to see if the Criteria is valid
195+
IList results = c.List();
194196

195-
s.Close();
197+
Assert.AreEqual(1, results.Count);
198+
}
196199
}
197200

198201
[Test]
199202
public void Hql()
200203
{
201204
// insert the new objects
202-
ISession s = OpenSession();
203-
ITransaction t = s.BeginTransaction();
204-
205-
ClassWithCompositeId theClass = new ClassWithCompositeId(id);
206-
theClass.OneProperty = 5;
207-
208-
ClassWithCompositeId theSecondClass = new ClassWithCompositeId(secondId);
209-
theSecondClass.OneProperty = 10;
210-
211-
s.Save(theClass);
212-
s.Save(theSecondClass);
205+
using (ISession s = OpenSession())
206+
using (ITransaction t = s.BeginTransaction())
207+
{
208+
ClassWithCompositeId theClass = new ClassWithCompositeId(id);
209+
theClass.OneProperty = 5;
213210

214-
t.Commit();
215-
s.Close();
211+
ClassWithCompositeId theSecondClass = new ClassWithCompositeId(secondId);
212+
theSecondClass.OneProperty = 10;
216213

217-
ISession s2 = OpenSession();
214+
s.Save(theClass);
215+
s.Save(theSecondClass);
218216

219-
IQuery hql = s2.CreateQuery("from ClassWithCompositeId as cwid where cwid.Id.KeyString = :keyString");
217+
t.Commit();
218+
}
220219

221-
hql.SetString("keyString", id.KeyString);
220+
using (ISession s2 = OpenSession())
221+
{
222+
IQuery hql = s2.CreateQuery("from ClassWithCompositeId as cwid where cwid.Id.KeyString = :keyString");
222223

223-
IList results = hql.List();
224+
hql.SetString("keyString", id.KeyString);
224225

225-
Assert.AreEqual(1, results.Count);
226+
IList results = hql.List();
226227

227-
s2.Close();
228+
Assert.AreEqual(1, results.Count);
229+
}
228230
}
229231
}
230-
}
232+
}

0 commit comments

Comments
 (0)