spring-orm

org.springframework.orm.hibernate3
Class AbstractSessionFactoryBean

java.lang.Object
  extended by org.springframework.orm.hibernate3.HibernateExceptionTranslator
      extended by org.springframework.orm.hibernate3.AbstractSessionFactoryBean
All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>, org.springframework.beans.factory.InitializingBean, org.springframework.dao.support.PersistenceExceptionTranslator
Direct Known Subclasses:
LocalSessionFactoryBean

public abstract class AbstractSessionFactoryBean
extends HibernateExceptionTranslator
implements org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean

Abstract FactoryBean that creates a Hibernate SessionFactory within a Spring application context, providing general infrastructure not related to Hibernate's specific configuration API.

This class implements the PersistenceExceptionTranslator interface, as autodetected by Spring's org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor, for AOP-based translation of native exceptions to Spring DataAccessExceptions. Hence, the presence of e.g. LocalSessionFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate Hibernate exceptions.

This class mainly serves as common base class for LocalSessionFactoryBean. For details on typical SessionFactory setup, see the LocalSessionFactoryBean javadoc.

Since:
2.0
Author:
Juergen Hoeller
See Also:
setExposeTransactionAwareSessionFactory(boolean), SessionFactory.getCurrentSession(), org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor

Field Summary
protected  org.apache.commons.logging.Log logger
          Logger available to subclasses
 
Constructor Summary
AbstractSessionFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Build and expose the SessionFactory.
protected  void afterSessionFactoryCreation()
          Hook that allows post-processing after the SessionFactory has been successfully created.
protected  void beforeSessionFactoryDestruction()
          Hook that allows shutdown processing before the SessionFactory will be closed.
protected abstract  org.hibernate.SessionFactory buildSessionFactory()
          Build the underlying Hibernate SessionFactory.
 void destroy()
          Close the SessionFactory on bean factory shutdown.
 javax.sql.DataSource getDataSource()
          Return the DataSource to be used by the SessionFactory.
 org.hibernate.SessionFactory getObject()
          Return the singleton SessionFactory.
 java.lang.Class<? extends org.hibernate.SessionFactory> getObjectType()
           
protected  org.hibernate.SessionFactory getSessionFactory()
          Return the exposed SessionFactory.
protected  boolean isExposeTransactionAwareSessionFactory()
          Return whether to expose a transaction-aware proxy for the SessionFactory.
 boolean isSingleton()
           
protected  boolean isUseTransactionAwareDataSource()
          Return whether to use a transaction-aware DataSource for the SessionFactory.
 void setDataSource(javax.sql.DataSource dataSource)
          Set the DataSource to be used by the SessionFactory.
 void setExposeTransactionAwareSessionFactory(boolean exposeTransactionAwareSessionFactory)
          Set whether to expose a transaction-aware current Session from the SessionFactory's getCurrentSession() method, returning the Session that's associated with the current Spring-managed transaction, if any.
 void setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource)
          Set whether to use a transaction-aware DataSource for the SessionFactory, i.e.
protected  org.hibernate.SessionFactory wrapSessionFactoryIfNecessary(org.hibernate.SessionFactory rawSf)
          Wrap the given SessionFactory with a proxy, if demanded.
 
Methods inherited from class org.springframework.orm.hibernate3.HibernateExceptionTranslator
convertHibernateAccessException, setJdbcExceptionTranslator, translateExceptionIfPossible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Logger available to subclasses

Constructor Detail

AbstractSessionFactoryBean

public AbstractSessionFactoryBean()
Method Detail

setDataSource

public void setDataSource(javax.sql.DataSource dataSource)
Set the DataSource to be used by the SessionFactory. If set, this will override corresponding settings in Hibernate properties.

If this is set, the Hibernate settings should not define a connection provider to avoid meaningless double configuration.

If using HibernateTransactionManager as transaction strategy, consider proxying your target DataSource with a LazyConnectionDataSourceProxy. This defers fetching of an actual JDBC Connection until the first JDBC Statement gets executed, even within JDBC transactions (as performed by HibernateTransactionManager). Such lazy fetching is particularly beneficial for read-only operations, in particular if the chances of resolving the result in the second-level cache are high.

As JTA and transactional JNDI DataSources already provide lazy enlistment of JDBC Connections, LazyConnectionDataSourceProxy does not add value with JTA (i.e. Spring's JtaTransactionManager) as transaction strategy.

See Also:
setUseTransactionAwareDataSource(boolean), HibernateTransactionManager, JtaTransactionManager, LazyConnectionDataSourceProxy

getDataSource

public javax.sql.DataSource getDataSource()
Return the DataSource to be used by the SessionFactory.


setUseTransactionAwareDataSource

public void setUseTransactionAwareDataSource(boolean useTransactionAwareDataSource)
Set whether to use a transaction-aware DataSource for the SessionFactory, i.e. whether to automatically wrap the passed-in DataSource with Spring's TransactionAwareDataSourceProxy.

Default is "false": LocalSessionFactoryBean is usually used with Spring's HibernateTransactionManager or JtaTransactionManager, both of which work nicely on a plain JDBC DataSource. Hibernate Sessions and their JDBC Connections are fully managed by the Hibernate/JTA transaction infrastructure in such a scenario.

If you switch this flag to "true", Spring's Hibernate access will be able to participate in JDBC-based transactions managed outside of Hibernate (for example, by Spring's DataSourceTransactionManager). This can be convenient if you need a different local transaction strategy for another O/R mapping tool, for example, but still want Hibernate access to join into those transactions.

A further benefit of this option is that plain Sessions opened directly via the SessionFactory, outside of Spring's Hibernate support, will still participate in active Spring-managed transactions. However, consider using Hibernate's getCurrentSession() method instead (see javadoc of "exposeTransactionAwareSessionFactory" property).

WARNING: When using a transaction-aware JDBC DataSource in combination with OpenSessionInViewFilter/Interceptor, whether participating in JTA or external JDBC-based transactions, it is strongly recommended to set Hibernate's Connection release mode to "after_transaction" or "after_statement", which guarantees proper Connection handling in such a scenario. In contrast to that, HibernateTransactionManager generally requires release mode "on_close".

Note: If you want to use Hibernate's Connection release mode "after_statement" with a DataSource specified on this LocalSessionFactoryBean (for example, a JTA-aware DataSource fetched from JNDI), switch this setting to "true". Otherwise, the ConnectionProvider used underneath will vote against aggressive release and thus silently switch to release mode "after_transaction".

See Also:
setDataSource(javax.sql.DataSource), setExposeTransactionAwareSessionFactory(boolean), TransactionAwareDataSourceProxy, DataSourceTransactionManager, OpenSessionInViewFilter, OpenSessionInViewInterceptor, HibernateTransactionManager, JtaTransactionManager

isUseTransactionAwareDataSource

protected boolean isUseTransactionAwareDataSource()
Return whether to use a transaction-aware DataSource for the SessionFactory.


setExposeTransactionAwareSessionFactory

public void setExposeTransactionAwareSessionFactory(boolean exposeTransactionAwareSessionFactory)
Set whether to expose a transaction-aware current Session from the SessionFactory's getCurrentSession() method, returning the Session that's associated with the current Spring-managed transaction, if any.

Default is "true", letting data access code work with the plain Hibernate SessionFactory and its getCurrentSession() method, while still being able to participate in current Spring-managed transactions: with any transaction management strategy, either local or JTA / EJB CMT, and any transaction synchronization mechanism, either Spring or JTA. Furthermore, getCurrentSession() will also seamlessly work with a request-scoped Session managed by OpenSessionInViewFilter/Interceptor.

Turn this flag off to expose the plain Hibernate SessionFactory with Hibernate's default getCurrentSession() behavior, supporting plain JTA synchronization only. Alternatively, simply override the corresponding Hibernate property "hibernate.current_session_context_class".

See Also:
SpringSessionContext, SessionFactory.getCurrentSession(), JtaTransactionManager, HibernateTransactionManager, OpenSessionInViewFilter, OpenSessionInViewInterceptor

isExposeTransactionAwareSessionFactory

protected boolean isExposeTransactionAwareSessionFactory()
Return whether to expose a transaction-aware proxy for the SessionFactory.


afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Build and expose the SessionFactory.

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception
See Also:
buildSessionFactory(), wrapSessionFactoryIfNecessary(org.hibernate.SessionFactory)

wrapSessionFactoryIfNecessary

protected org.hibernate.SessionFactory wrapSessionFactoryIfNecessary(org.hibernate.SessionFactory rawSf)
Wrap the given SessionFactory with a proxy, if demanded.

The default implementation simply returns the given SessionFactory as-is. Subclasses may override this to implement transaction awareness through a SessionFactory proxy, for example.

Parameters:
rawSf - the raw SessionFactory as built by buildSessionFactory()
Returns:
the SessionFactory reference to expose
See Also:
buildSessionFactory()

getSessionFactory

protected final org.hibernate.SessionFactory getSessionFactory()
Return the exposed SessionFactory. Will throw an exception if not initialized yet.

Returns:
the SessionFactory (never null)
Throws:
java.lang.IllegalStateException - if the SessionFactory has not been initialized yet

destroy

public void destroy()
             throws org.hibernate.HibernateException
Close the SessionFactory on bean factory shutdown.

Specified by:
destroy in interface org.springframework.beans.factory.DisposableBean
Throws:
org.hibernate.HibernateException

getObject

public org.hibernate.SessionFactory getObject()
Return the singleton SessionFactory.

Specified by:
getObject in interface org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>

getObjectType

public java.lang.Class<? extends org.hibernate.SessionFactory> getObjectType()
Specified by:
getObjectType in interface org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>

isSingleton

public boolean isSingleton()
Specified by:
isSingleton in interface org.springframework.beans.factory.FactoryBean<org.hibernate.SessionFactory>

buildSessionFactory

protected abstract org.hibernate.SessionFactory buildSessionFactory()
                                                             throws java.lang.Exception
Build the underlying Hibernate SessionFactory.

Returns:
the raw SessionFactory (potentially to be wrapped with a transaction-aware proxy before it is exposed to the application)
Throws:
java.lang.Exception - in case of initialization failure

afterSessionFactoryCreation

protected void afterSessionFactoryCreation()
                                    throws java.lang.Exception
Hook that allows post-processing after the SessionFactory has been successfully created. The SessionFactory is already available through getSessionFactory() at this point.

This implementation is empty.

Throws:
java.lang.Exception - in case of initialization failure
See Also:
getSessionFactory()

beforeSessionFactoryDestruction

protected void beforeSessionFactoryDestruction()
Hook that allows shutdown processing before the SessionFactory will be closed. The SessionFactory is still available through getSessionFactory() at this point.

This implementation is empty.

See Also:
getSessionFactory()

spring-orm