C# Class Spring.Data.NHibernate.SessionFactoryUtils

Helper class featuring methods for Hibernate Session handling, allowing for reuse of Hibernate Session instances within transactions. Also provides support for exception translation.
Mostra file Open project: spring-projects/spring-net Class Usage Examples

Public Properties

Property Type Description
SESSION_SYNCHRONIZATION_ORDER int

Public Methods

Method Description
ApplyTransactionTimeout ( ICriteria criteria, ISessionFactory sessionFactory ) : void

Applies the current transaction timeout, if any, to the given criteria object

ApplyTransactionTimeout ( IQuery query, ISessionFactory sessionFactory ) : void

Applies the current transaction timeout, if any, to the given Hibenrate query object.

CloseSession ( ISession session ) : void

Perform the actual closing of the Hibernate Session catching and logging any cleanup exceptions thrown.

ConvertAdoAccessException ( IAdoExceptionTranslator translator, ADOException ex ) : Spring.Dao.DataAccessException

Converts a Hibernate ADOException to a Spring DataAccessExcption, extracting the underlying error code from ADO.NET. Will extract the ADOException Message and SqlString properties and pass them to the translate method of the provided IAdoExceptionTranslator.

ConvertHibernateAccessException ( NHibernate.HibernateException ex ) : Spring.Dao.DataAccessException

Convert the given HibernateException to an appropriate exception from the Spring.Dao hierarchy. Note that it is advisable to handle AdoException specifically by using a AdoExceptionTranslator for the underlying ADO.NET exception.

DoGetSession ( ISessionFactory sessionFactory, bool allowCreate ) : ISession

Get a Hibernate Session for the given SessionFactory.

Is aware of and will return any existing corresponding Session bound to the current thread, for example whenusing HibernateTransactionManager. Will create a new Session otherwise, if "allowCreate" is true.

Throws the orginal HibernateException, in contrast to GetSession(ISessionFactory, bool).

GetDbProvider ( ISessionFactory sessionFactory ) : IDbProvider

Gets the Spring IDbProvider given the ISessionFactory.

The matching is performed by comparing the assembly qualified name string of the hibernate Driver.ConnectionType to those in the DbProviderFactory definitions. No connections are created in performing this comparison.

GetNewSession ( ISessionFactory sessionFactory, IInterceptor interceptor ) : ISession

Get a new Hibernate Session from the given SessionFactory. Will return a new Session even if there already is a pre-bound Session for the given SessionFactory.

Within a transaction, this method will create a new Session that shares the transaction's ADO.NET Connection. More specifically, it will use the same ADO.NET Connection as the pre-bound Hibernate Session.

GetSession ( ISessionFactory sessionFactory, IInterceptor entityInterceptor, IAdoExceptionTranslator adoExceptionTranslator ) : ISession

Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using HibernateTransactionManager. Will always create a new Session otherwise.

Supports setting a Session-level Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database. Such an interceptor can also be set at the SessionFactory level (i.e. on LocalSessionFactoryObject), on HibernateTransactionManager, or on HibernateInterceptor/HibernateTemplate.

GetSession ( ISessionFactory sessionFactory, bool allowCreate ) : ISession

Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using HibernateTransactionManager. Will create a new Session otherwise, if allowCreate is true.

InitDeferredClose ( ISessionFactory sessionFactory ) : void

Initialize deferred close for the current thread and the given SessionFactory. Sessions will not be actually closed on close calls then, but rather at a processDeferredClose call at a finishing point (like request completion).

IsDeferredCloseActive ( ISessionFactory sessionFactory ) : bool

Return if deferred close is active for the current thread and the given SessionFactory.

IsSessionTransactional ( ISession session, ISessionFactory sessionFactory ) : bool

Return whether the given Hibernate Session is transactional, that is, bound to the current thread by Spring's transaction facilities.

NewAdoExceptionTranslator ( ISessionFactory sessionFactory ) : IAdoExceptionTranslator

Create a IAdoExceptionTranslator from the given SessionFactory.

If a corresponding IDbProvider is found, a ErrorcodeExceptionTranslator for the IDbProvider is created. Otherwise, a FallbackException is created.

ProcessDeferredClose ( ISessionFactory sessionFactory ) : void

Process Sessions that have been registered for deferred close for the given SessionFactory.

ReleaseSession ( ISession session, ISessionFactory sessionFactory ) : void

Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread).

SessionFactoryUtils ( ) : System

Initializes a new instance of the SessionFactoryUtils class.

Private Methods

Method Description
CloseSessionOrRegisterDeferredClose ( ISession session, ISessionFactory sessionFactory ) : void

Close the given Session or register it for deferred close.

DoGetSession ( ISessionFactory sessionFactory, IInterceptor entityInterceptor, IAdoExceptionTranslator adoExceptionTranslator, bool allowCreate ) : ISession
GetSession ( ISessionFactory sessionFactory, IInterceptor entityInterceptor, IAdoExceptionTranslator adoExceptionTranslator, bool allowCreate ) : ISession
OpenSession ( ISessionFactory sessionFactory, IInterceptor entityInterceptor ) : ISession

Open a new Session from the factory.

Method Details

ApplyTransactionTimeout() public static method

Applies the current transaction timeout, if any, to the given criteria object
If criteria argument is null.
public static ApplyTransactionTimeout ( ICriteria criteria, ISessionFactory sessionFactory ) : void
criteria ICriteria The Hibernate Criteria object.
sessionFactory ISessionFactory Hibernate SessionFactory that the Criteria was created for /// (can be null).
return void

ApplyTransactionTimeout() public static method

Applies the current transaction timeout, if any, to the given Hibenrate query object.
If query argument is null.
public static ApplyTransactionTimeout ( IQuery query, ISessionFactory sessionFactory ) : void
query IQuery The Hibernate Query object.
sessionFactory ISessionFactory Hibernate SessionFactory that the Query was created for /// (can be null).
return void

CloseSession() public static method

Perform the actual closing of the Hibernate Session catching and logging any cleanup exceptions thrown.
public static CloseSession ( ISession session ) : void
session ISession The hibernate session to close
return void

ConvertAdoAccessException() public static method

Converts a Hibernate ADOException to a Spring DataAccessExcption, extracting the underlying error code from ADO.NET. Will extract the ADOException Message and SqlString properties and pass them to the translate method of the provided IAdoExceptionTranslator.
public static ConvertAdoAccessException ( IAdoExceptionTranslator translator, ADOException ex ) : Spring.Dao.DataAccessException
translator IAdoExceptionTranslator The IAdoExceptionTranslator, may be a user provided implementation as configured on /// HibernateTemplate. ///
ex NHibernate.ADOException The ADOException throw
return Spring.Dao.DataAccessException

ConvertHibernateAccessException() public static method

Convert the given HibernateException to an appropriate exception from the Spring.Dao hierarchy. Note that it is advisable to handle AdoException specifically by using a AdoExceptionTranslator for the underlying ADO.NET exception.
public static ConvertHibernateAccessException ( NHibernate.HibernateException ex ) : Spring.Dao.DataAccessException
ex NHibernate.HibernateException The Hibernate exception that occured.
return Spring.Dao.DataAccessException

DoGetSession() public static method

Get a Hibernate Session for the given SessionFactory.
Is aware of and will return any existing corresponding Session bound to the current thread, for example whenusing HibernateTransactionManager. Will create a new Session otherwise, if "allowCreate" is true.

Throws the orginal HibernateException, in contrast to GetSession(ISessionFactory, bool).

/// if the Session couldn't be created /// /// If no thread-bound Session found and allowCreate is false. ///
public static DoGetSession ( ISessionFactory sessionFactory, bool allowCreate ) : ISession
sessionFactory ISessionFactory The session factory.
allowCreate bool if set to true [allow create].
return ISession

GetDbProvider() public static method

Gets the Spring IDbProvider given the ISessionFactory.
The matching is performed by comparing the assembly qualified name string of the hibernate Driver.ConnectionType to those in the DbProviderFactory definitions. No connections are created in performing this comparison.
If DbProviderFactory's ApplicaitonContext is not /// an instance of IConfigurableApplicaitonContext.
public static GetDbProvider ( ISessionFactory sessionFactory ) : IDbProvider
sessionFactory ISessionFactory The session factory.
return IDbProvider

GetNewSession() public static method

Get a new Hibernate Session from the given SessionFactory. Will return a new Session even if there already is a pre-bound Session for the given SessionFactory.
Within a transaction, this method will create a new Session that shares the transaction's ADO.NET Connection. More specifically, it will use the same ADO.NET Connection as the pre-bound Hibernate Session.
If could not open Hibernate session
public static GetNewSession ( ISessionFactory sessionFactory, IInterceptor interceptor ) : ISession
sessionFactory ISessionFactory The session factory to create the session with.
interceptor IInterceptor The Hibernate entity interceptor, or null if none.
return ISession

GetSession() public static method

Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using HibernateTransactionManager. Will always create a new Session otherwise.
Supports setting a Session-level Hibernate entity interceptor that allows to inspect and change property values before writing to and reading from the database. Such an interceptor can also be set at the SessionFactory level (i.e. on LocalSessionFactoryObject), on HibernateTransactionManager, or on HibernateInterceptor/HibernateTemplate.
/// If the session couldn't be created. /// /// If no thread-bound Session found and allowCreate is false. ///
public static GetSession ( ISessionFactory sessionFactory, IInterceptor entityInterceptor, IAdoExceptionTranslator adoExceptionTranslator ) : ISession
sessionFactory ISessionFactory The session factory to create the /// session with.
entityInterceptor IInterceptor Hibernate entity interceptor, or null if none.
adoExceptionTranslator IAdoExceptionTranslator AdoExceptionTranslator to use for flushing the /// Session on transaction synchronization (can be null; only used when actually /// registering a transaction synchronization).
return ISession

GetSession() public static method

Get a Hibernate Session for the given SessionFactory. Is aware of and will return any existing corresponding Session bound to the current thread, for example when using HibernateTransactionManager. Will create a new Session otherwise, if allowCreate is true.
/// If the session couldn't be created. /// /// If no thread-bound Session found and allowCreate is false. ///
public static GetSession ( ISessionFactory sessionFactory, bool allowCreate ) : ISession
sessionFactory ISessionFactory The session factory to create the session with.
allowCreate bool if set to true create a non-transactional Session when no /// transactional Session can be found for the current thread.
return ISession

InitDeferredClose() public static method

Initialize deferred close for the current thread and the given SessionFactory. Sessions will not be actually closed on close calls then, but rather at a processDeferredClose call at a finishing point (like request completion).
public static InitDeferredClose ( ISessionFactory sessionFactory ) : void
sessionFactory ISessionFactory The session factory.
return void

IsDeferredCloseActive() public static method

Return if deferred close is active for the current thread and the given SessionFactory.
If SessionFactory argument is null.
public static IsDeferredCloseActive ( ISessionFactory sessionFactory ) : bool
sessionFactory ISessionFactory The session factory.
return bool

IsSessionTransactional() public static method

Return whether the given Hibernate Session is transactional, that is, bound to the current thread by Spring's transaction facilities.
public static IsSessionTransactional ( ISession session, ISessionFactory sessionFactory ) : bool
session ISession The hibernate session to check
sessionFactory ISessionFactory The session factory that the session /// was created with, can be null.
return bool

NewAdoExceptionTranslator() public static method

Create a IAdoExceptionTranslator from the given SessionFactory.
If a corresponding IDbProvider is found, a ErrorcodeExceptionTranslator for the IDbProvider is created. Otherwise, a FallbackException is created.
public static NewAdoExceptionTranslator ( ISessionFactory sessionFactory ) : IAdoExceptionTranslator
sessionFactory ISessionFactory The session factory to create the translator for
return IAdoExceptionTranslator

ProcessDeferredClose() public static method

Process Sessions that have been registered for deferred close for the given SessionFactory.
If there is no session factory associated with the thread.
public static ProcessDeferredClose ( ISessionFactory sessionFactory ) : void
sessionFactory ISessionFactory The session factory.
return void

ReleaseSession() public static method

Close the given Session, created via the given factory, if it is not managed externally (i.e. not bound to the thread).
public static ReleaseSession ( ISession session, ISessionFactory sessionFactory ) : void
session ISession The hibernate session to close
sessionFactory ISessionFactory The hibernate SessionFactory that /// the session was created with.
return void

SessionFactoryUtils() public method

Initializes a new instance of the SessionFactoryUtils class.
public SessionFactoryUtils ( ) : System
return System

Property Details

SESSION_SYNCHRONIZATION_ORDER public_oe static_oe property

The ordering value for synchronizaiton this session resources. Set to be lower than ADO.NET synchronization.
public static int SESSION_SYNCHRONIZATION_ORDER
return int