org.springframework.orm.toplink
Class SessionFactoryUtils

java.lang.Object
  extended by org.springframework.orm.toplink.SessionFactoryUtils

public abstract class SessionFactoryUtils
extends Object

Helper class featuring methods for TopLink Session handling, allowing for reuse of TopLink Session instances within transactions.

Supports synchronization with both Spring-managed JTA transactions (i.e. JtaTransactionManager) and non-Spring JTA transactions (i.e. plain JTA or EJB CMT). See the getSession version with all parameters for details.

Used internally by TopLinkTemplate and TopLinkInterceptor. Can also be used directly in application code, e.g. in combination with TopLinkInterceptor.

Since:
1.2
Author:
Juergen Hoeller, James Clark
See Also:
TopLinkTemplate, TopLinkInterceptor, TopLinkTransactionManager, JtaTransactionManager

Constructor Summary
SessionFactoryUtils()
           
 
Method Summary
static DataAccessException convertTopLinkAccessException(oracle.toplink.exceptions.TopLinkException ex)
          Convert the given TopLinkException to an appropriate exception from the org.springframework.dao hierarchy.
static oracle.toplink.sessions.Session doGetSession(SessionFactory sessionFactory, boolean allowCreate)
          Get a TopLink Session for the given SessionFactory.
static oracle.toplink.sessions.Session getSession(SessionFactory sessionFactory, boolean allowCreate)
          Get a TopLink Session for the given SessionFactory.
static void releaseSession(oracle.toplink.sessions.Session session, SessionFactory sessionFactory)
          Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SessionFactoryUtils

public SessionFactoryUtils()
Method Detail

getSession

public static oracle.toplink.sessions.Session getSession(SessionFactory sessionFactory,
                                                         boolean allowCreate)
                                                  throws DataAccessResourceFailureException,
                                                         IllegalStateException
Get a TopLink Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using TopLinkTransactionManager. 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. Note that TopLinkTemplate allows to write data access code without caring about such resource handling.

Parameters:
sessionFactory - TopLink SessionFactory to create the session with
allowCreate - if a non-transactional Session should be created when no transactional Session can be found for the current thread
Returns:
the TopLink Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
IllegalStateException - if no thread-bound Session found and allowCreate false
See Also:
releaseSession(oracle.toplink.sessions.Session, org.springframework.orm.toplink.SessionFactory), TopLinkTemplate

doGetSession

public static oracle.toplink.sessions.Session doGetSession(SessionFactory sessionFactory,
                                                           boolean allowCreate)
                                                    throws oracle.toplink.exceptions.TopLinkException,
                                                           IllegalStateException
Get a TopLink Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using TopLinkTransactionManager. Will create a new Session otherwise, if allowCreate is true.

Same as getSession, but throwing the original TopLinkException.

Parameters:
sessionFactory - TopLink SessionFactory to create the session with
allowCreate - if a non-transactional Session should be created when no transactional Session can be found for the current thread
Returns:
the TopLink Session
Throws:
oracle.toplink.exceptions.TopLinkException - if the Session couldn't be created
IllegalStateException - if no thread-bound Session found and allowCreate false
See Also:
releaseSession(oracle.toplink.sessions.Session, org.springframework.orm.toplink.SessionFactory), TopLinkTemplate

convertTopLinkAccessException

public static DataAccessException convertTopLinkAccessException(oracle.toplink.exceptions.TopLinkException ex)
Convert the given TopLinkException to an appropriate exception from the org.springframework.dao hierarchy.

Parameters:
ex - TopLinkException that occured
Returns:
the corresponding DataAccessException instance

releaseSession

public static void releaseSession(oracle.toplink.sessions.Session session,
                                  SessionFactory sessionFactory)
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 TopLink Session to close
sessionFactory - TopLink SessionFactory that the Session was created with (can be null)


Copyright (c) 2002-2005 The Spring Framework Project.