The Spring Framework

org.springframework.orm.toplink
Class LocalSessionFactoryBean

java.lang.Object
  extended by org.springframework.orm.toplink.LocalSessionFactory
      extended by org.springframework.orm.toplink.LocalSessionFactoryBean
All Implemented Interfaces:
BeanClassLoaderAware, DisposableBean, FactoryBean, InitializingBean, PersistenceExceptionTranslator

public class LocalSessionFactoryBean
extends LocalSessionFactory
implements FactoryBean, BeanClassLoaderAware, InitializingBean, DisposableBean, PersistenceExceptionTranslator

FactoryBean that creates a TopLink SessionFactory. This is the usual way to set up a shared TopLink SessionFactory in a Spring application context; the SessionFactory can then be passed to TopLink-based DAOs via dependency injection.

See the base class LocalSessionFactory for configuration details.

This class also implements the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor, for AOP-based translation of native exceptions to Spring DataAccessExceptions. Hence, the presence of a LocalSessionFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate TopLink exceptions.

If your DAOs expect to receive a raw TopLink Session, consider defining a TransactionAwareSessionAdapter in front of this bean. This adapter will provide a TopLink Session rather than a SessionFactory as bean reference. Your DAOs can then, for example, access the currently active Session and UnitOfWork via Session.getActiveSession() and Session.getActiveUnitOfWork(), respectively. Note that you can still access the SessionFactory as well, by defining a bean reference that points directly at the LocalSessionFactoryBean.

Since:
1.2
Author:
Juergen Hoeller
See Also:
LocalSessionFactory, TransactionAwareSessionAdapter, PersistenceExceptionTranslationPostProcessor

Field Summary
 
Fields inherited from class org.springframework.orm.toplink.LocalSessionFactory
DEFAULT_SESSION_NAME, DEFAULT_SESSIONS_XML, logger
 
Constructor Summary
LocalSessionFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
 DataAccessException convertTopLinkAccessException(oracle.toplink.exceptions.TopLinkException ex)
          Convert the given TopLinkException to an appropriate exception from the org.springframework.dao hierarchy.
 void destroy()
          Invoked by a BeanFactory on destruction of a singleton.
 SQLExceptionTranslator getJdbcExceptionTranslator()
          Return the JDBC exception translator for this instance, if any.
 Object getObject()
          Return an instance (possibly shared or independent) of the object managed by this factory.
 Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
 boolean isSingleton()
          Is the object managed by this factory a singleton?
 void setBeanClassLoader(ClassLoader classLoader)
          Sets the given bean ClassLoader as TopLink Session ClassLoader.
 void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)
          Set the JDBC exception translator for this SessionFactory.
 DataAccessException translateExceptionIfPossible(RuntimeException ex)
          Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
 
Methods inherited from class org.springframework.orm.toplink.LocalSessionFactory
createSessionFactory, getLoginPropertyMap, getSessionManager, loadDatabaseSession, newSessionFactory, setConfigLocation, setDatabaseLogin, setDatabaseLogin, setDatabasePlatform, setDataSource, setLoginProperties, setLoginPropertyMap, setSessionClassLoader, setSessionLog, setSessionName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocalSessionFactoryBean

public LocalSessionFactoryBean()
Method Detail

setJdbcExceptionTranslator

public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator)
Set the JDBC exception translator for this SessionFactory.

Applied to any SQLException root cause of a TopLink DatabaseException, within Spring's PersistenceExceptionTranslator mechanism. The default is to rely on TopLink's native exception translation.

See Also:
DatabaseException, SQLErrorCodeSQLExceptionTranslator, SQLStateSQLExceptionTranslator

getJdbcExceptionTranslator

public SQLExceptionTranslator getJdbcExceptionTranslator()
Return the JDBC exception translator for this instance, if any.


setBeanClassLoader

public void setBeanClassLoader(ClassLoader classLoader)
Sets the given bean ClassLoader as TopLink Session ClassLoader.

Specified by:
setBeanClassLoader in interface BeanClassLoaderAware
Parameters:
classLoader - the owning class loader; may be null in which case a default ClassLoader must be used, for example the ClassLoader obtained via ClassUtils.getDefaultClassLoader()
See Also:
LocalSessionFactory.setSessionClassLoader(java.lang.ClassLoader)

afterPropertiesSet

public void afterPropertiesSet()
                        throws oracle.toplink.exceptions.TopLinkException
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
oracle.toplink.exceptions.TopLinkException

getObject

public Object getObject()
Description copied from interface: FactoryBean
Return an instance (possibly shared or independent) of the object managed by this factory.

As with a BeanFactory, this allows support for both the Singleton and Prototype design pattern.

If this FactoryBean is not fully initialized yet at the time of the call (for example because it is involved in a circular reference), throw a corresponding FactoryBeanNotInitializedException.

As of Spring 2.0, FactoryBeans are allowed to return null objects. The factory will consider this as normal value to be used; it will not throw a FactoryBeanNotInitializedException in this case anymore. FactoryBean implementations are encouraged to throw FactoryBeanNotInitializedException themselves now, as appropriate.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (can be null)
See Also:
FactoryBeanNotInitializedException

getObjectType

public Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance.

This allows one to check for specific types of beans without instantiating objects, for example on autowiring.

In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class)

isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the object managed by this factory a singleton? That is, will FactoryBean.getObject() always return the same object (a reference that can be cached)?

NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

NOTE: This method returning false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its SmartFactoryBean.isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

Specified by:
isSingleton in interface FactoryBean
Returns:
whether the exposed object is a singleton
See Also:
FactoryBean.getObject(), SmartFactoryBean.isPrototype()

translateExceptionIfPossible

public DataAccessException translateExceptionIfPossible(RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.

Converts the exception if it is a TopLinkException; else returns null to indicate an unknown exception.

Specified by:
translateExceptionIfPossible in interface PersistenceExceptionTranslator
Parameters:
ex - a RuntimeException thrown
Returns:
the corresponding DataAccessException (or null if the exception could not be translated, as in this case it may result from user code rather than an actual persistence problem)
See Also:
PersistenceExceptionTranslationPostProcessor, convertTopLinkAccessException(oracle.toplink.exceptions.TopLinkException)

convertTopLinkAccessException

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

Will automatically apply a specified SQLExceptionTranslator to a TopLink DatabaseException, else rely on TopLink's default translation.

Parameters:
ex - TopLinkException that occured
Returns:
a corresponding DataAccessException
See Also:
SessionFactoryUtils.convertTopLinkAccessException(oracle.toplink.exceptions.TopLinkException), setJdbcExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)

destroy

public void destroy()
Description copied from interface: DisposableBean
Invoked by a BeanFactory on destruction of a singleton.

Specified by:
destroy in interface DisposableBean

The Spring Framework

Copyright © 2002-2007 The Spring Framework.