org.picocontainer.persistence.hibernate
Class ScopedSession

java.lang.Object
  extended by org.picocontainer.persistence.hibernate.AbstractSessionDecorator
      extended by org.picocontainer.persistence.hibernate.ScopedSession
All Implemented Interfaces:
Serializable, org.hibernate.Session, org.picocontainer.Disposable, org.picocontainer.Startable

public final class ScopedSession
extends AbstractSessionDecorator
implements org.picocontainer.Startable, org.picocontainer.Disposable

Session implementation which allows request scoping while supporting lifecycle events. If you register this component within either a request PicoContainer or a request-scoped storage object, then this component will close sessions whenever stop() is called by the container, and is reusable until dispose() is called.

This allows for the "One Session Per Request" pattern often used in Hibernate.

Author:
Jose Peleteiro, Michael Rimov, Mauro Talevi
See Also:
Serialized Form

Constructor Summary
ScopedSession(org.hibernate.SessionFactory factory)
          Creates a ScopedSession with factory and null interceptor
ScopedSession(org.hibernate.SessionFactory factory, org.hibernate.Interceptor interceptor)
          Creates a ScopedSession with factory and interceptor
 
Method Summary
 Connection close()
           *
 void dispose()
          
protected  void finalize()
          Add some insurance against potential memory leaks.
 org.hibernate.Session getDelegate()
          Obtain hibernate session in lazy way.
 org.hibernate.Interceptor getInterceptor()
          Returns the current interceptor.
 org.hibernate.SessionFactory getSessionFactory()
           *
 int hashCode()
           *
 void invalidateDelegate()
          Perform actions to dispose "burned" session properly.
 void setInterceptor(org.hibernate.Interceptor interceptor)
          Sets a new hibernate session interceptor.
 void start()
          
 void stop()
          
 String toString()
           *
 
Methods inherited from class org.picocontainer.persistence.hibernate.AbstractSessionDecorator
beginTransaction, cancelQuery, clear, connection, contains, createCriteria, createCriteria, createCriteria, createCriteria, createFilter, createQuery, createSQLQuery, delete, delete, disableFilter, disconnect, enableFilter, evict, flush, get, get, get, get, getCacheMode, getCurrentLockMode, getEnabledFilter, getEntityMode, getEntityName, getFlushMode, getIdentifier, getNamedQuery, getSession, getStatistics, getTransaction, handleException, isConnected, isDirty, isOpen, load, load, load, load, load, lock, lock, merge, merge, persist, persist, reconnect, reconnect, refresh, refresh, replicate, replicate, save, save, saveOrUpdate, saveOrUpdate, setCacheMode, setFlushMode, setReadOnly, update, update
 
Methods inherited from class java.lang.Object
clone, equals, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ScopedSession

public ScopedSession(org.hibernate.SessionFactory factory)
Creates a ScopedSession with factory and null interceptor

Parameters:
factory - session factory to create the session

ScopedSession

public ScopedSession(org.hibernate.SessionFactory factory,
                     org.hibernate.Interceptor interceptor)
Creates a ScopedSession with factory and interceptor

Parameters:
factory - session factory to create the session
interceptor - interceptor to use with created session
Method Detail

getSessionFactory

public org.hibernate.SessionFactory getSessionFactory()
*

Specified by:
getSessionFactory in interface org.hibernate.Session
Overrides:
getSessionFactory in class AbstractSessionDecorator

getDelegate

public org.hibernate.Session getDelegate()
Obtain hibernate session in lazy way.

Specified by:
getDelegate in class AbstractSessionDecorator
Returns:
The Session

close

public Connection close()
*

Because this implementation decorates a delegate session, it removes the delegate session, but it does allow re-referencing once close() has been called. It simply grabs a new Hibernate session.

Specified by:
close in interface org.hibernate.Session
Overrides:
close in class AbstractSessionDecorator

invalidateDelegate

public void invalidateDelegate()
Description copied from class: AbstractSessionDecorator
Perform actions to dispose "burned" session properly.

Specified by:
invalidateDelegate in class AbstractSessionDecorator

getInterceptor

public org.hibernate.Interceptor getInterceptor()
Returns the current interceptor.

Returns:
The Interceptor

setInterceptor

public void setInterceptor(org.hibernate.Interceptor interceptor)
                    throws IllegalStateException
Sets a new hibernate session interceptor. This is only applicable if there is no current session. If this session object has been used, then please call close() first.

Parameters:
interceptor - the Interceptor to apply to this session.
Throws:
IllegalStateException - if this session has already been utilized after creation.

finalize

protected void finalize()
                 throws Throwable
Add some insurance against potential memory leaks. Make sure that Session is closed.

Overrides:
finalize in class Object
Throws:
Throwable

hashCode

public int hashCode()
*

Overrides:
hashCode in class Object

toString

public String toString()
*

Overrides:
toString in class Object

start

public void start()

Currently does nothing. Session is lazily created .

Specified by:
start in interface org.picocontainer.Startable

stop

public void stop()

Closes and invalidates any sessions that are still open.

Specified by:
stop in interface org.picocontainer.Startable

dispose

public void dispose()

Prevents any further utilization once called.

Specified by:
dispose in interface org.picocontainer.Disposable


Copyright © 2008. All Rights Reserved.