FileDocCategorySizeDatePackage
ThreadLocalCurrentSessionTest.javaAPI DocHibernate 3.2.53578Tue Dec 12 16:22:26 GMT 2006org.hibernate.test.connections

ThreadLocalCurrentSessionTest

public class ThreadLocalCurrentSessionTest extends ConnectionManagementTestCase
author
Steve Ebersole

Fields Summary
Constructors Summary
public ThreadLocalCurrentSessionTest(String name)

		super( name );
	
Methods Summary
protected voidcheckDeserializedState(org.hibernate.Session session)

		assertTrue( "session not bound after deserialize", TestableThreadLocalContext.isSessionBound( session ) );
	
protected voidcheckSerializedState(org.hibernate.Session session)

		assertFalse( "session still bound after serialize", TestableThreadLocalContext.isSessionBound( session ) );
	
public voidconfigure(org.hibernate.cfg.Configuration cfg)

		super.configure( cfg );
		cfg.setProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS, TestableThreadLocalContext.class.getName() );
		cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
	
protected org.hibernate.SessiongetSessionUnderTest()

		Session session = getSessions().getCurrentSession();
		session.beginTransaction();
		return session;
	
protected voidreconnect(org.hibernate.Session session)

//		session.reconnect();
		session.beginTransaction();
	
protected voidrelease(org.hibernate.Session session)

		long initialCount = getSessions().getStatistics().getSessionCloseCount();
		session.getTransaction().commit();
		long subsequentCount = getSessions().getStatistics().getSessionCloseCount();
		assertEquals( "Session still open after commit", initialCount + 1, subsequentCount );
		// also make sure it was cleaned up from the internal ThreadLocal...
		assertFalse( "session still bound to internal ThreadLocal", TestableThreadLocalContext.hasBind() );
	
public static junit.framework.Testsuite()

		return new FunctionalTestClassTestSuite( ThreadLocalCurrentSessionTest.class );
	
public voidtestContextCleanup()

		Session session = getSessions().getCurrentSession();
		session.beginTransaction();
		session.getTransaction().commit();
		assertFalse( "session open after txn completion", session.isOpen() );
		assertFalse( "session still bound after txn completion", TestableThreadLocalContext.isSessionBound( session ) );

		Session session2 = getSessions().getCurrentSession();
		assertFalse( "same session returned after txn completion", session == session2 );
		session2.close();
		assertFalse( "session open after closing", session2.isOpen() );
		assertFalse( "session still bound after closing", TestableThreadLocalContext.isSessionBound( session2 ) );
	
public voidtestTransactionProtection()

		Session session = getSessions().getCurrentSession();
		try {
			session.createQuery( "from Silly" );
			fail( "method other than beginTransaction{} allowed" );
		}
		catch ( HibernateException e ) {
			// ok
		}