Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.hibernate.context
Class ThreadLocalSessionContext  view ThreadLocalSessionContext download ThreadLocalSessionContext.java

java.lang.Object
  extended byorg.hibernate.context.ThreadLocalSessionContext
All Implemented Interfaces:
CurrentSessionContext, java.io.Serializable

public class ThreadLocalSessionContext
extends java.lang.Object
implements CurrentSessionContext

A CurrentSessionContext impl which scopes the notion of current session by the current thread of execution. Unlike the JTA counterpart, threads do not give us a nice hook to perform any type of cleanup making it questionable for this impl to actually generate Session instances. In the interest of usability, it was decided to have this default impl actually generate a session upon first request and then clean it up after the org.hibernate.Transaction associated with that session is committed/rolled-back. In order for ensuring that happens, the sessions generated here are unusable until after Session.beginTransaction()>Session.beginTransaction() 55 has been called. If close() is called on a session managed by this class, it will be automatically unbound.

Additionally, the static bind(org.hibernate.Session) 55 and unbind(org.hibernate.SessionFactory) 55 methods are provided to allow application code to explicitly control opening and closing of these sessions. This, with some from of interception, is the preferred approach. It also allows easy framework integration and one possible approach for implementing long-sessions.

The buildOrObtainSession() 55 , isAutoCloseEnabled() 55 , isAutoFlushEnabled() 55 , getConnectionReleaseMode() 55 , and buildCleanupSynch() 55 methods are all provided to allow easy subclassing (for long- running session scenarios, for example).


Nested Class Summary
protected static class ThreadLocalSessionContext.CleanupSynch
          JTA transaction synch used for cleanup of the internal session map.
private  class ThreadLocalSessionContext.TransactionProtectionWrapper
           
 
Field Summary
private static java.lang.ThreadLocal context
          A ThreadLocal maintaining current sessions for the given execution thread.
protected  org.hibernate.engine.SessionFactoryImplementor factory
           
private static org.apache.commons.logging.Log log
           
private static java.lang.Class[] SESS_PROXY_INTERFACES
           
 
Constructor Summary
ThreadLocalSessionContext(org.hibernate.engine.SessionFactoryImplementor factory)
           
 
Method Summary
static void bind(org.hibernate.Session session)
          Associates the given session with the current thread of execution.
protected  ThreadLocalSessionContext.CleanupSynch buildCleanupSynch()
           
protected  org.hibernate.classic.Session buildOrObtainSession()
          Strictly provided for subclassing purposes; specifically to allow long-session support.
private static void cleanupAnyOrphanedSession(org.hibernate.SessionFactory factory)
           
 org.hibernate.classic.Session currentSession()
          Retrieve the current session according to the scoping defined by this implementation.
private static void doBind(org.hibernate.Session session, org.hibernate.SessionFactory factory)
           
private static org.hibernate.classic.Session doUnbind(org.hibernate.SessionFactory factory, boolean releaseMapIfEmpty)
           
private static org.hibernate.classic.Session existingSession(org.hibernate.SessionFactory factory)
           
protected  org.hibernate.ConnectionReleaseMode getConnectionReleaseMode()
          Mainly for subclass usage.
protected  org.hibernate.engine.SessionFactoryImplementor getFactory()
           
protected  boolean isAutoCloseEnabled()
          Mainly for subclass usage.
protected  boolean isAutoFlushEnabled()
          Mainly for subclass usage.
private  boolean needsWrapping(org.hibernate.classic.Session session)
           
protected static java.util.Map sessionMap()
           
static org.hibernate.classic.Session unbind(org.hibernate.SessionFactory factory)
          Unassociate a previously bound session from the current thread of execution.
protected  org.hibernate.classic.Session wrap(org.hibernate.classic.Session session)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log

SESS_PROXY_INTERFACES

private static final java.lang.Class[] SESS_PROXY_INTERFACES

context

private static final java.lang.ThreadLocal context
A ThreadLocal maintaining current sessions for the given execution thread. The actual ThreadLocal variable is a java.util.Map to account for the possibility for multiple SessionFactorys being used during execution of the given thread.


factory

protected final org.hibernate.engine.SessionFactoryImplementor factory
Constructor Detail

ThreadLocalSessionContext

public ThreadLocalSessionContext(org.hibernate.engine.SessionFactoryImplementor factory)
Method Detail

currentSession

public final org.hibernate.classic.Session currentSession()
                                                   throws org.hibernate.HibernateException
Description copied from interface: CurrentSessionContext
Retrieve the current session according to the scoping defined by this implementation.

Specified by:
currentSession in interface CurrentSessionContext

needsWrapping

private boolean needsWrapping(org.hibernate.classic.Session session)

getFactory

protected org.hibernate.engine.SessionFactoryImplementor getFactory()

buildOrObtainSession

protected org.hibernate.classic.Session buildOrObtainSession()
Strictly provided for subclassing purposes; specifically to allow long-session support.

This implementation always just opens a new session.


buildCleanupSynch

protected ThreadLocalSessionContext.CleanupSynch buildCleanupSynch()

isAutoCloseEnabled

protected boolean isAutoCloseEnabled()
Mainly for subclass usage. This impl always returns true.


isAutoFlushEnabled

protected boolean isAutoFlushEnabled()
Mainly for subclass usage. This impl always returns true.


getConnectionReleaseMode

protected org.hibernate.ConnectionReleaseMode getConnectionReleaseMode()
Mainly for subclass usage. This impl always returns after_transaction.


wrap

protected org.hibernate.classic.Session wrap(org.hibernate.classic.Session session)

bind

public static void bind(org.hibernate.Session session)
Associates the given session with the current thread of execution.


cleanupAnyOrphanedSession

private static void cleanupAnyOrphanedSession(org.hibernate.SessionFactory factory)

unbind

public static org.hibernate.classic.Session unbind(org.hibernate.SessionFactory factory)
Unassociate a previously bound session from the current thread of execution.


existingSession

private static org.hibernate.classic.Session existingSession(org.hibernate.SessionFactory factory)

sessionMap

protected static java.util.Map sessionMap()

doBind

private static void doBind(org.hibernate.Session session,
                           org.hibernate.SessionFactory factory)

doUnbind

private static org.hibernate.classic.Session doUnbind(org.hibernate.SessionFactory factory,
                                                      boolean releaseMapIfEmpty)