Skip to content

Commit a989542

Browse files
author
Sergey Koshcheyev
committed
Removed ConnectionReleaseMode.AfterStatement - it appears to make no sense on .NET.
Added a test. SVN: trunk@2533
1 parent 89c1777 commit a989542

15 files changed

+507
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
using System;
2+
using System.Collections;
3+
using System.Data;
4+
using NHibernate.Cfg;
5+
using NHibernate.Util;
6+
using NUnit.Framework;
7+
using Environment=NHibernate.Cfg.Environment;
8+
9+
namespace NHibernate.Test.ConnectionTest
10+
{
11+
[TestFixture]
12+
public class AggressiveReleaseTest : ConnectionManagementTestCase
13+
{
14+
protected override void Configure(Configuration cfg)
15+
{
16+
base.Configure(cfg);
17+
cfg.SetProperty(Environment.ReleaseConnections, "after_transaction");
18+
//cfg.SetProperty(Environment.ConnectionProvider, typeof(DummyConnectionProvider).AssemblyQualifiedName);
19+
//cfg.SetProperty(Environment.GenerateStatistics, "true");
20+
cfg.SetProperty(Environment.BatchSize, "0");
21+
}
22+
23+
protected override ISession GetSessionUnderTest()
24+
{
25+
return OpenSession();
26+
}
27+
28+
protected void Reconnect(ISession session)
29+
{
30+
session.Reconnect();
31+
}
32+
33+
protected void Prepare()
34+
{
35+
//DummyTransactionManager.INSTANCE.Begin();
36+
}
37+
38+
protected void Done()
39+
{
40+
//DummyTransactionManager.INSTANCE.Commit();
41+
}
42+
43+
// Some additional tests specifically for the aggressive-Release functionality...
44+
45+
[Test]
46+
public void SerializationOnAfterStatementAggressiveRelease()
47+
{
48+
Prepare();
49+
ISession s = GetSessionUnderTest();
50+
Silly silly = new Silly("silly");
51+
s.Save(silly);
52+
53+
// this should cause the CM to obtain a connection, and then Release it
54+
s.Flush();
55+
56+
// We should be able to serialize the session at this point...
57+
SerializationHelper.Serialize(s);
58+
59+
s.Delete(silly);
60+
s.Flush();
61+
62+
Release(s);
63+
Done();
64+
}
65+
66+
[Test]
67+
public void SerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources()
68+
{
69+
Prepare();
70+
ISession s = GetSessionUnderTest();
71+
72+
Silly silly = new Silly("silly");
73+
s.Save(silly);
74+
75+
// this should cause the CM to obtain a connection, and then Release it
76+
s.Flush();
77+
78+
// both scroll() and iterate() cause the batcher to hold on
79+
// to resources, which should make aggresive-Release not Release
80+
// the connection (and thus cause serialization to fail)
81+
IEnumerable en = s.CreateQuery("from Silly").Enumerable();
82+
83+
try
84+
{
85+
SerializationHelper.Serialize(s);
86+
Assert.Fail("Serialization allowed on connected session; or aggressive Release released connection with open resources");
87+
}
88+
catch (InvalidOperationException e)
89+
{
90+
// expected behavior
91+
}
92+
93+
// Closing the ScrollableResults does currently force the batcher to
94+
// aggressively Release the connection
95+
NHibernateUtil.Close(en);
96+
SerializationHelper.Serialize(s);
97+
98+
s.Delete(silly);
99+
s.Flush();
100+
101+
Release(s);
102+
Done();
103+
}
104+
105+
[Test]
106+
public void QueryIteration()
107+
{
108+
Prepare();
109+
ISession s = GetSessionUnderTest();
110+
Silly silly = new Silly("silly");
111+
s.Save(silly);
112+
s.Flush();
113+
114+
IEnumerable en = s.CreateQuery("from Silly").Enumerable();
115+
IEnumerator itr = en.GetEnumerator();
116+
Assert.IsTrue(itr.MoveNext());
117+
Silly silly2 = (Silly) itr.Current;
118+
Assert.AreEqual(silly, silly2);
119+
NHibernateUtil.Close(itr);
120+
121+
itr = s.CreateQuery("from Silly").Enumerable().GetEnumerator();
122+
IEnumerator itr2 = s.CreateQuery("from Silly where name = 'silly'").Enumerable().GetEnumerator();
123+
124+
Assert.IsTrue(itr.MoveNext());
125+
Assert.AreEqual(silly, itr.Current);
126+
Assert.IsTrue(itr2.MoveNext());
127+
Assert.AreEqual(silly, itr2.Current);
128+
129+
NHibernateUtil.Close(itr);
130+
NHibernateUtil.Close(itr2);
131+
132+
s.Delete(silly);
133+
s.Flush();
134+
135+
Release(s);
136+
Done();
137+
}
138+
139+
//[Test]
140+
//public void QueryScrolling()
141+
//{
142+
// Prepare();
143+
// ISession s = GetSessionUnderTest();
144+
// Silly silly = new Silly("silly");
145+
// s.Save(silly);
146+
// s.Flush();
147+
148+
// ScrollableResults sr = s.CreateQuery("from Silly").scroll();
149+
// Assert.IsTrue(sr.next());
150+
// Silly silly2 = (Silly) sr.get(0);
151+
// Assert.AreEqual(silly, silly2);
152+
// sr.Close();
153+
154+
// sr = s.CreateQuery("from Silly").Scroll();
155+
// ScrollableResults sr2 = s.CreateQuery("from Silly where name = 'silly'").Scroll();
156+
157+
// Assert.IsTrue(sr.next());
158+
// Assert.AreEqual(silly, sr.get(0));
159+
// Assert.IsTrue(sr2.next());
160+
// Assert.AreEqual(silly, sr2.get(0));
161+
162+
// sr.Close();
163+
// sr2.Close();
164+
165+
// s.Delete(silly);
166+
// s.Flush();
167+
168+
// Release(s);
169+
// Done();
170+
//}
171+
172+
[Test]
173+
public void SuppliedConnection()
174+
{
175+
Prepare();
176+
177+
IDbConnection originalConnection = sessions.ConnectionProvider.GetConnection();
178+
ISession session = sessions.OpenSession(originalConnection);
179+
180+
Silly silly = new Silly("silly");
181+
session.Save(silly);
182+
183+
// this will cause the connection manager to cycle through the aggressive Release logic;
184+
// it should not Release the connection since we explicitly suplied it ourselves.
185+
session.Flush();
186+
187+
Assert.IsTrue(originalConnection == session.Connection, "Different connections");
188+
189+
session.Delete(silly);
190+
session.Flush();
191+
192+
Release(session);
193+
originalConnection.Close();
194+
Done();
195+
}
196+
197+
// TODO
198+
//[Test]
199+
//public void BorrowedConnections()
200+
//{
201+
// Prepare();
202+
// ISession s = GetSessionUnderTest();
203+
204+
// IDbConnection conn = s.Connection;
205+
// Assert.IsTrue(((SessionImpl) s).ConnectionManager.HasBorrowedConnection);
206+
// conn.Close();
207+
// Assert.IsFalse(((SessionImpl) s).ConnectionManager.HasBorrowedConnection);
208+
209+
// Release(s);
210+
// Done();
211+
//}
212+
213+
[Test]
214+
public void ConnectionMaintanenceDuringFlush()
215+
{
216+
Prepare();
217+
ISession s = GetSessionUnderTest();
218+
s.BeginTransaction();
219+
220+
IList entities = new ArrayList();
221+
for (int i = 0; i < 10; i++)
222+
{
223+
Other other = new Other("other-" + i);
224+
Silly silly = new Silly("silly-" + i, other);
225+
entities.Add(silly);
226+
s.Save(silly);
227+
}
228+
s.Flush();
229+
230+
foreach (Silly silly in entities)
231+
{
232+
silly.Name = "new-" + silly.Name;
233+
silly.Other.Name = "new-" + silly.Other.Name;
234+
}
235+
// long initialCount = sessions.Statistics.getConnectCount();
236+
s.Flush();
237+
// Assert.AreEqual(initialCount + 1, sessions.Statistics.getConnectCount(), "connection not maintained through Flush");
238+
239+
s.Delete("from Silly");
240+
s.Delete("from Other");
241+
s.Transaction.Commit();
242+
Release(s);
243+
Done();
244+
}
245+
}
246+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections;
3+
4+
namespace NHibernate.Test.ConnectionTest
5+
{
6+
public abstract class ConnectionManagementTestCase : TestCase
7+
{
8+
protected override IList Mappings
9+
{
10+
get { return new string[] { "ConnectionTest.Silly.hbm.xml" }; }
11+
}
12+
13+
protected override string MappingsAssembly
14+
{
15+
get
16+
{
17+
return "NHibernate.Test";
18+
}
19+
}
20+
21+
protected virtual void Prepare()
22+
{
23+
}
24+
25+
protected virtual void Done()
26+
{
27+
}
28+
29+
protected abstract ISession GetSessionUnderTest();
30+
31+
protected virtual void Release(ISession session)
32+
{
33+
if (session != null && session.IsOpen)
34+
{
35+
try
36+
{
37+
session.Close();
38+
}
39+
catch
40+
{
41+
// Ignore
42+
}
43+
}
44+
}
45+
}
46+
}

Diff for: src/NHibernate.Test/ConnectionTest/Other.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace NHibernate.Test.ConnectionTest
4+
{
5+
[Serializable]
6+
public class Other
7+
{
8+
private long id;
9+
private string name;
10+
11+
public Other()
12+
{
13+
}
14+
15+
public Other(string name)
16+
{
17+
this.name = name;
18+
}
19+
20+
public virtual long Id
21+
{
22+
get { return id; }
23+
set { id = value; }
24+
}
25+
26+
public virtual string Name
27+
{
28+
get { return name; }
29+
set { name = value; }
30+
}
31+
}
32+
}

Diff for: src/NHibernate.Test/ConnectionTest/Silly.cs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
3+
namespace NHibernate.Test.ConnectionTest
4+
{
5+
[Serializable]
6+
public class Silly
7+
{
8+
private long id;
9+
private string name;
10+
private Other other;
11+
12+
public Silly()
13+
{
14+
}
15+
16+
public Silly(string name)
17+
{
18+
this.name = name;
19+
}
20+
21+
public Silly(string name, Other other)
22+
{
23+
this.name = name;
24+
this.other = other;
25+
}
26+
27+
public virtual long Id
28+
{
29+
get { return id; }
30+
set { id = value; }
31+
}
32+
33+
public virtual string Name
34+
{
35+
get { return name; }
36+
set { name = value; }
37+
}
38+
39+
public virtual Other Other
40+
{
41+
get { return other; }
42+
set { other = value; }
43+
}
44+
}
45+
}

Diff for: src/NHibernate.Test/ConnectionTest/Silly.hbm.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.ConnectionTest">
5+
<class name="Silly">
6+
<id name="Id" type="long">
7+
<generator class="native"/>
8+
</id>
9+
<property name="Name"/>
10+
<many-to-one name="Other" class="Other" cascade="all"/>
11+
</class>
12+
13+
<class name="Other">
14+
<id name="Id" type="long">
15+
<generator class="native"/>
16+
</id>
17+
<property name="Name"/>
18+
</class>
19+
</hibernate-mapping>

0 commit comments

Comments
 (0)