Source code: org/hibernate/test/connections/ThreadLocalCurrentSessionTest.java
1 package org.hibernate.test.connections;
2
3 import org.hibernate.Session;
4 import org.hibernate.HibernateException;
5 import org.hibernate.engine.SessionFactoryImplementor;
6 import org.hibernate.context.ThreadLocalSessionContext;
7 import org.hibernate.cfg.Configuration;
8 import org.hibernate.cfg.Environment;
9
10 /**
11 * @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
12 */
13 public class ThreadLocalCurrentSessionTest extends ConnectionManagementTestCase {
14
15 public ThreadLocalCurrentSessionTest(String name) {
16 super( name );
17 }
18
19 protected void configure(Configuration cfg) {
20 super.configure( cfg );
21 cfg.setProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS, TestableThreadLocalContext.class.getName() );
22 cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
23 }
24
25 protected Session getSessionUnderTest() throws Throwable {
26 Session session = getSessions().getCurrentSession();
27 session.beginTransaction();
28 return session;
29 }
30
31 protected void release(Session session) {
32 long initialCount = getSessions().getStatistics().getSessionCloseCount();
33 session.getTransaction().commit();
34 long subsequentCount = getSessions().getStatistics().getSessionCloseCount();
35 assertEquals( "Session still open after commit", initialCount + 1, subsequentCount );
36 // also make sure it was cleaned up from the internal ThreadLocal...
37 assertFalse( "session still bound to internal ThreadLocal", TestableThreadLocalContext.hasBind() );
38 }
39
40 protected void reconnect(Session session) throws Throwable {
41 // session.reconnect();
42 session.beginTransaction();
43 }
44
45 protected void checkSerializedState(Session session) {
46 assertFalse( "session still bound after serialize", TestableThreadLocalContext.isSessionBound( session ) );
47 }
48
49 protected void checkDeserializedState(Session session) {
50 assertTrue( "session not bound after deserialize", TestableThreadLocalContext.isSessionBound( session ) );
51 }
52
53 public void testTransactionProtection() {
54 Session session = getSessions().getCurrentSession();
55 try {
56 session.createQuery( "from Silly" );
57 fail( "method other than beginTransaction{} allowed" );
58 }
59 catch( HibernateException e ) {
60 // ok
61 }
62 }
63
64 public void testContextCleanup() {
65 Session session = getSessions().getCurrentSession();
66 session.beginTransaction();
67 session.getTransaction().commit();
68 assertFalse( "session open after txn completion", session.isOpen() );
69 assertFalse( "session still bound after txn completion", TestableThreadLocalContext.isSessionBound( session ) );
70
71 Session session2 = getSessions().getCurrentSession();
72 assertFalse( "same session returned after txn completion", session == session2 );
73 session2.close();
74 assertFalse( "session open after closing", session2.isOpen() );
75 assertFalse( "session still bound after closing", TestableThreadLocalContext.isSessionBound( session2 ) );
76 }
77
78 public static class TestableThreadLocalContext extends ThreadLocalSessionContext {
79 private static TestableThreadLocalContext me;
80 public TestableThreadLocalContext(SessionFactoryImplementor factory) {
81 super( factory );
82 me = this;
83 }
84 public static boolean isSessionBound(Session session) {
85 return sessionMap() != null && sessionMap().containsKey( me.factory )
86 && sessionMap().get( me.factory ) == session;
87 }
88 public static boolean hasBind() {
89 return sessionMap() != null && sessionMap().containsKey( me.factory );
90 }
91 }
92 }