it.amattioli.dominate.hibernate
Class HibernateSessionManager

Show UML class diagram
java.lang.Object
  extended by it.amattioli.dominate.hibernate.HibernateSessionManager
All Implemented Interfaces:
SessionManager

public class HibernateSessionManager
extends Object
implements SessionManager

Can be used to retrieve a Hibernate session with a defined life-cycle.

Two different life-cycles are possible: long running and thread local

Sessions with a thread local life cycle are created and destroyed in the current thread, wile long-running sessions, at the end of the current thread, disconnect their database connection and can be reconnected in a new thread.

Author:
a.mattioli

Constructor Summary
HibernateSessionManager()
           
HibernateSessionManager(SessionMode sessionMode)
          Creates a new session managers
 
Method Summary
static void disconnectAll()
          Terminates all the sessions used in the current thread.
static String getCfgResource()
           
static org.hibernate.cfg.Configuration getConfiguration()
          Retrieves the Configuration object that will be used for Hibernate configuration.
 org.hibernate.Session getSession()
          Retrieves the session managed by this session manager.
<S> S
getSession(Class<S> sessionClass)
          Retrieves a session associated with this SessionManager.
static org.hibernate.SessionFactory getSessionFactory()
          Retrieves the Hibernate SessionFactory that will be used to create new sessions.
 SessionMode getSessionMode()
          Retrieves the SessionMode associated with this SessionManager
 boolean hasSessionMode(SessionMode sMode)
          Check if this SessionManager has a certain SessionMode
 boolean isReleased()
          Check if this SessionManager has been released.
 void release()
          Informs this manager that it will not be used any more so it can definitely close the session it manages
 void reset()
          Close the current session and prepare to open a new one when the getSession() method is called.
static void setCfgResource(String cfgResource)
          Set the name of the resource xml file from which the Hibernate configuration should be read.
static void setConfiguration(org.hibernate.cfg.Configuration newCfg)
          The Configuration object that will be used for Hibernate configuration.
 void setSessionMode(SessionMode sessionMode)
           
 org.apache.commons.collections.Closure transactionalClosure(org.apache.commons.collections.Closure toBeDone)
          Decorate a closure so that it will be executed in a transactional context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateSessionManager

public HibernateSessionManager()

HibernateSessionManager

public HibernateSessionManager(SessionMode sessionMode)
Creates a new session managers

Parameters:
sessionMode - the life cycle of the managed session
Method Detail

setConfiguration

public static void setConfiguration(org.hibernate.cfg.Configuration newCfg)
The Configuration object that will be used for Hibernate configuration. If a previous configuration was set that will be discarded and the new one will be used to create e new SessionFactory.

Parameters:
newCfg - the Configuration object that will be used for Hibernate configuration

getConfiguration

public static org.hibernate.cfg.Configuration getConfiguration()
Retrieves the Configuration object that will be used for Hibernate configuration. If a Configuration object was set using setConfiguration(Configuration) that object will be returned, otherwise a new default Configuration object will be created.

Returns:
the Configuration object that will be used for Hibernate configuration

getCfgResource

public static String getCfgResource()

setCfgResource

public static void setCfgResource(String cfgResource)
Set the name of the resource xml file from which the Hibernate configuration should be read. If a different resource is set any previously created configuration will be discarded and the new one will be used to create a new SessionFactory.

Parameters:
cfgResource -

getSessionFactory

public static org.hibernate.SessionFactory getSessionFactory()
Retrieves the Hibernate SessionFactory that will be used to create new sessions.

Returns:
the Hibernate SessionFactory

disconnectAll

public static void disconnectAll()
Terminates all the sessions used in the current thread. On long running sessions the Session.disconnect() method will be called, so they can be re-opened, while on thread local sessions the Session.close() method will be called and they cannot be used again.


setSessionMode

public void setSessionMode(SessionMode sessionMode)
Specified by:
setSessionMode in interface SessionManager

getSessionMode

public SessionMode getSessionMode()
Description copied from interface: SessionManager
Retrieves the SessionMode associated with this SessionManager

Specified by:
getSessionMode in interface SessionManager
Returns:
the SessionMode associated with this SessionManager

hasSessionMode

public boolean hasSessionMode(SessionMode sMode)
Description copied from interface: SessionManager
Check if this SessionManager has a certain SessionMode

Specified by:
hasSessionMode in interface SessionManager
Parameters:
sMode - the SessionMode to check
Returns:
true if this SessionManager has a certain SessionMode

isReleased

public boolean isReleased()
Description copied from interface: SessionManager
Check if this SessionManager has been released.

Specified by:
isReleased in interface SessionManager
Returns:
true if this SessionManager has been released, false otherwise
See Also:
#release()}

getSession

public <S> S getSession(Class<S> sessionClass)
Description copied from interface: SessionManager
Retrieves a session associated with this SessionManager. If this SessionManager has an associated session whose class is the same as the given class it will be returned, otherwise null will be returned.

Specified by:
getSession in interface SessionManager
Parameters:
sessionClass - the class of the needed session
Returns:
the session associated with this SessionManager whose class is the same as the given class if it exists, null otherwise

getSession

public org.hibernate.Session getSession()
Retrieves the session managed by this session manager.

Returns:
the managed Hibernate session

release

public void release()
Informs this manager that it will not be used any more so it can definitely close the session it manages

Specified by:
release in interface SessionManager

reset

public void reset()
Close the current session and prepare to open a new one when the getSession() method is called.

Specified by:
reset in interface SessionManager

transactionalClosure

public org.apache.commons.collections.Closure transactionalClosure(org.apache.commons.collections.Closure toBeDone)
Description copied from interface: SessionManager
Decorate a closure so that it will be executed in a transactional context. A transaction will be open before the closure is executed and the same transaction will be committed after the execution.

Specified by:
transactionalClosure in interface SessionManager
Parameters:
toBeDone - the closure to be decorated
Returns:
the decorated closure


Copyright © 2011. All Rights Reserved.