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.
Afficher le fichier Open project: spring-projects/spring-net Class Usage Examples

Méthodes publiques

Свойство Type Description
SESSION_SYNCHRONIZATION_ORDER int

Méthodes publiques

Méthode 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

Méthode 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 méthode

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).
Résultat void

ApplyTransactionTimeout() public static méthode

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).
Résultat void

CloseSession() public static méthode

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
Résultat void

ConvertAdoAccessException() public static méthode

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
Résultat Spring.Dao.DataAccessException

ConvertHibernateAccessException() public static méthode

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.
Résultat Spring.Dao.DataAccessException

DoGetSession() public static méthode

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].
Résultat ISession

GetDbProvider() public static méthode

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.
Résultat IDbProvider

GetNewSession() public static méthode

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.
Résultat ISession

GetSession() public static méthode

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).
Résultat ISession

GetSession() public static méthode

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.
Résultat ISession

InitDeferredClose() public static méthode

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.
Résultat void

IsDeferredCloseActive() public static méthode

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.
Résultat bool

IsSessionTransactional() public static méthode

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.
Résultat bool

NewAdoExceptionTranslator() public static méthode

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
Résultat IAdoExceptionTranslator

ProcessDeferredClose() public static méthode

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.
Résultat void

ReleaseSession() public static méthode

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.
Résultat void

SessionFactoryUtils() public méthode

Initializes a new instance of the SessionFactoryUtils class.
public SessionFactoryUtils ( ) : System
Résultat 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
Résultat int