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
+ }
0 commit comments