Skip to content

Commit fe9fffe

Browse files
committed
- Minor Test added for ThreadStaticSessionContext as Ignored.
- Preparing AutoClose Session. SVN: trunk@3332
1 parent 57752ee commit fe9fffe

File tree

5 files changed

+124
-6
lines changed

5 files changed

+124
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using NHibernate.Cfg;
2+
using NHibernate.Context;
3+
using NHibernate.Engine;
4+
using NUnit.Framework;
5+
6+
namespace NHibernate.Test.ConnectionTest
7+
{
8+
[TestFixture,Ignore("Not yet supported, need a better or diferent treatment at transactions. Need AutoClosed feature.")]
9+
public class ThreadLocalCurrentSessionTest : ConnectionManagementTestCase
10+
{
11+
protected override ISession GetSessionUnderTest()
12+
{
13+
ISession session = OpenSession();
14+
session.BeginTransaction();
15+
return session;
16+
}
17+
18+
protected override void Configure(Configuration configuration)
19+
{
20+
base.Configure(cfg);
21+
cfg.SetProperty(Environment.CurrentSessionContextClass, typeof (TestableThreadStaticContext).AssemblyQualifiedName);
22+
cfg.SetProperty(Environment.GenerateStatistics, "true");
23+
}
24+
25+
protected override void Release(ISession session)
26+
{
27+
long initialCount = sessions.Statistics.SessionCloseCount;
28+
session.Transaction.Commit();
29+
long subsequentCount = sessions.Statistics.SessionCloseCount;
30+
Assert.AreEqual(initialCount + 1, subsequentCount, "Session still open after commit");
31+
// also make sure it was cleaned up from the internal ThreadLocal...
32+
Assert.IsFalse(TestableThreadStaticContext.HasBind(), "session still bound to internal ThreadLocal");
33+
}
34+
35+
//TODO: Need AutoCloseEnabled feature after commit.
36+
[Test]
37+
public void ContextCleanup()
38+
{
39+
ISession session = OpenSession();
40+
41+
session.BeginTransaction();
42+
session.Transaction.Commit();
43+
Assert.IsFalse(session.IsOpen, "session open after txn completion");
44+
Assert.IsFalse(TestableThreadStaticContext.IsSessionBound(session), "session still bound after txn completion");
45+
46+
ISession session2 = OpenSession();
47+
Assert.IsFalse(session.Equals(session2), "same session returned after txn completion");
48+
session2.Close();
49+
Assert.IsFalse(session2.IsOpen, "session open after closing");
50+
Assert.IsFalse(TestableThreadStaticContext.IsSessionBound(session2), "session still bound after closing");
51+
}
52+
53+
[Test]
54+
public void TransactionProtection()
55+
{
56+
using (ISession session = OpenSession())
57+
{
58+
try
59+
{
60+
session.CreateQuery("from Silly");
61+
Assert.Fail("method other than beginTransaction{} allowed");
62+
}
63+
catch (HibernateException e)
64+
{
65+
// ok
66+
}
67+
}
68+
}
69+
}
70+
71+
public class TestableThreadStaticContext : ThreadStaticSessionContext
72+
{
73+
private static TestableThreadStaticContext me;
74+
75+
public TestableThreadStaticContext(ISessionFactoryImplementor factory)
76+
: base(factory)
77+
{
78+
me = this;
79+
}
80+
81+
public static bool IsSessionBound(ISession session)
82+
{
83+
return context != null && context.ContainsKey(me.factory)
84+
&& context[me.factory] == session;
85+
}
86+
87+
public static bool HasBind()
88+
{
89+
return context != null && context.ContainsKey(me.factory);
90+
}
91+
}
92+
}

src/NHibernate.Test/NHibernate.Test-2.0.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="ConnectionTest\ConnectionManagementTestCase.cs" />
101101
<Compile Include="ConnectionTest\Other.cs" />
102102
<Compile Include="ConnectionTest\Silly.cs" />
103+
<Compile Include="ConnectionTest\ThreadLocalCurrentSessionTest.cs" />
103104
<Compile Include="Criteria\AddNumberProjection.cs" />
104105
<Compile Include="Criteria\Animal.cs" />
105106
<Compile Include="Criteria\Course.cs" />

src/NHibernate/Context/ThreadStaticSessionContext.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ThreadStaticSessionContext : ICurrentSessionContext
3232
{
3333
private static readonly ILog log = LogManager.GetLogger(typeof (ThreadStaticSessionContext));
3434

35-
[ThreadStatic] private static IDictionary<ISessionFactory, ISession> context;
35+
[ThreadStatic] protected static IDictionary<ISessionFactory, ISession> context;
3636

3737
protected readonly ISessionFactoryImplementor factory;
3838

@@ -49,7 +49,7 @@ public ISession CurrentSession()
4949
ISession current = ExistingSession(factory);
5050
if (current == null)
5151
{
52-
current = buildOrObtainSession();
52+
current = BuildOrObtainSession();
5353

5454
// wrap the session in the transaction-protection proxy
5555
if (NeedsWrapping(current))
@@ -146,7 +146,7 @@ private bool NeedsWrapping(ISession current)
146146
return false;
147147
}
148148

149-
protected ISession buildOrObtainSession()
149+
protected ISession BuildOrObtainSession()
150150
{
151151
return factory.OpenSession(
152152
null,

src/NHibernate/Impl/SessionImpl.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public sealed class SessionImpl : AbstractSessionImpl, IEventSource, ISerializab
7373
[NonSerialized]
7474
private IDictionary<EntityMode,ISession> childSessionsByEntityMode;
7575

76+
77+
//[NonSerialized] private bool flushBeforeCompletionEnabled;
78+
[NonSerialized] private bool autoCloseSessionEnabled;
79+
//[NonSerialized] private ConnectionReleaseMode connectionReleaseMode;
80+
7681
#region System.Runtime.Serialization.ISerializable Members
7782

7883
/// <summary>
@@ -222,7 +227,7 @@ public SessionImpl(
222227
this.actionQueue = new ActionQueue(this);
223228
this.persistenceContext = new StatefulPersistenceContext(this);
224229
//this.flushBeforeCompletionEnabled = flushBeforeCompletionEnabled;
225-
//this.autoCloseSessionEnabled = autoCloseSessionEnabled;
230+
this.autoCloseSessionEnabled = autoCloseSessionEnabled;
226231
//this.connectionReleaseMode = connectionReleaseMode;
227232
//this.jdbcContext = new JDBCContext(this, connection, interceptor);
228233
}
@@ -244,7 +249,7 @@ private SessionImpl(SessionImpl parent, EntityMode entityMode)
244249
this.entityMode = entityMode;
245250
this.persistenceContext = new StatefulPersistenceContext(this);
246251
//this.flushBeforeCompletionEnabled = false;
247-
//this.autoCloseSessionEnabled = false;
252+
this.autoCloseSessionEnabled = false;
248253
//this.connectionReleaseMode = null;
249254

250255
if (factory.Statistics.IsStatisticsEnabled)
@@ -269,6 +274,16 @@ public override long Timestamp
269274
get { return timestamp; }
270275
}
271276

277+
public bool IsAutoCloseSessionEnabled
278+
{
279+
get { return autoCloseSessionEnabled; }
280+
}
281+
282+
public bool ShouldAutoClose
283+
{
284+
get { return IsAutoCloseSessionEnabled && !IsClosed; }
285+
}
286+
272287
/// <summary></summary>
273288
public IDbConnection Close()
274289
{

src/NHibernate/Transaction/AdoTransaction.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void Commit()
164164
}
165165

166166
session.BeforeTransactionCompletion(this);
167-
167+
168168
try
169169
{
170170
trans.Commit();
@@ -267,6 +267,16 @@ public IsolationLevel IsolationLevel
267267
get { return trans.IsolationLevel; }
268268
}
269269

270+
void CloseIfRequerid()
271+
{
272+
//bool close = session.ShouldAutoClose() &&
273+
// !transactionContext.isClosed();
274+
//if (close)
275+
//{
276+
// transactionContext.managedClose();
277+
//}
278+
}
279+
270280
#region System.IDisposable Members
271281

272282
/// <summary>

0 commit comments

Comments
 (0)