org.springframework.datastore.core
Class DatastoreUtils

java.lang.Object
  extended by org.springframework.datastore.core.DatastoreUtils

public abstract class DatastoreUtils
extends Object

Helper class for obtaining Datastore sessions. Based on similar work for Hibernate such as SessionFactoryUtils


Field Summary
static org.apache.commons.logging.Log logger
           
 
Constructor Summary
DatastoreUtils()
           
 
Method Summary
static void closeSession(Session session)
          Perform actual closing of the Session, catching and logging any cleanup exceptions thrown.
static void closeSessionOrRegisterDeferredClose(Session session, Datastore datastore)
          Close the given Session or register it for deferred close.
static Session getSession(Datastore datastore, boolean allowCreate)
          Get a Datastore Session for the given Datastore.
static void initDeferredClose(Datastore datastore)
          Initialize deferred close for the current thread and the given Datastore.
static boolean isSessionTransactional(Session session, Datastore datastore)
          Return whether the given Datastore Session is transactional, that is, bound to the current thread by Spring's transaction facilities.
static void processDeferredClose(Datastore datastore)
          Process all Datastore Sessions that have been registered for deferred close for the given SessionFactory.
static void releaseSession(Session session, Datastore datastore)
          Close the given Session, created via the given factory, if it is not managed externally (i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

public static final org.apache.commons.logging.Log logger
Constructor Detail

DatastoreUtils

public DatastoreUtils()
Method Detail

getSession

public static Session getSession(Datastore datastore,
                                 boolean allowCreate)
                          throws org.springframework.dao.DataAccessResourceFailureException,
                                 IllegalStateException
Get a Datastore Session for the given Datastore. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using DatastoreTransactionManager. Will create a new Session otherwise, if "allowCreate" is true.

This is the getSession method used by typical data access code, in combination with releaseSession called when done with the Session.

Parameters:
datastore - Datastore to create the session with
allowCreate - whether a non-transactional Session should be created when no transactional Session can be found for the current thread
Returns:
the Datastore Session
Throws:
org.springframework.dao.DataAccessResourceFailureException - if the Session couldn't be created
IllegalStateException - if no thread-bound Session found and "allowCreate" is false

isSessionTransactional

public static boolean isSessionTransactional(Session session,
                                             Datastore datastore)
Return whether the given Datastore Session is transactional, that is, bound to the current thread by Spring's transaction facilities.

Parameters:
session - the Datastore Session to check
datastore - Datastore that the Session was created with (may be null)
Returns:
whether the Session is transactional

closeSession

public static void closeSession(Session session)
Perform actual closing of the Session, catching and logging any cleanup exceptions thrown.

Parameters:
session - The Session instance

releaseSession

public static void releaseSession(Session session,
                                  Datastore datastore)
Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread).

Parameters:
session - the Datastore Session to close (may be null)
datastore - Datastore that the Session was created with (may be null)

processDeferredClose

public static void processDeferredClose(Datastore datastore)
Process all Datastore Sessions that have been registered for deferred close for the given SessionFactory.

Parameters:
datastore - the Datastore to process deferred close for
See Also:
initDeferredClose(org.springframework.datastore.core.Datastore), releaseSession(org.springframework.datastore.core.Session, org.springframework.datastore.core.Datastore)

initDeferredClose

public static void initDeferredClose(Datastore datastore)
Initialize deferred close for the current thread and the given Datastore. Sessions will not be actually closed on close calls then, but rather at a processDeferredClose(org.springframework.datastore.core.Datastore) call at a finishing point (like request completion).

Parameters:
datastore - the Datastore to initialize deferred close for
See Also:
processDeferredClose(org.springframework.datastore.core.Datastore), releaseSession(org.springframework.datastore.core.Session, org.springframework.datastore.core.Datastore)

closeSessionOrRegisterDeferredClose

public static void closeSessionOrRegisterDeferredClose(Session session,
                                                       Datastore datastore)
Close the given Session or register it for deferred close.

Parameters:
session - the Datastore Session to close
datastore - Datastore that the Session was created with (may be null)
See Also:
initDeferredClose(org.springframework.datastore.core.Datastore), processDeferredClose(org.springframework.datastore.core.Datastore)