C# Класс 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.
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
SESSION_SYNCHRONIZATION_ORDER int

Открытые методы

Метод Описание
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.

Приватные методы

Метод Описание
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.

Описание методов

ApplyTransactionTimeout() публичный статический Метод

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).
Результат void

ApplyTransactionTimeout() публичный статический Метод

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).
Результат void

CloseSession() публичный статический Метод

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
Результат void

ConvertAdoAccessException() публичный статический Метод

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
Результат Spring.Dao.DataAccessException

ConvertHibernateAccessException() публичный статический Метод

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.
Результат Spring.Dao.DataAccessException

DoGetSession() публичный статический Метод

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].
Результат ISession

GetDbProvider() публичный статический Метод

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.
Результат IDbProvider

GetNewSession() публичный статический Метод

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.
Результат ISession

GetSession() публичный статический Метод

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).
Результат ISession

GetSession() публичный статический Метод

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.
Результат ISession

InitDeferredClose() публичный статический Метод

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.
Результат void

IsDeferredCloseActive() публичный статический Метод

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.
Результат bool

IsSessionTransactional() публичный статический Метод

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.
Результат bool

NewAdoExceptionTranslator() публичный статический Метод

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
Результат IAdoExceptionTranslator

ProcessDeferredClose() публичный статический Метод

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.
Результат void

ReleaseSession() публичный статический Метод

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.
Результат void

SessionFactoryUtils() публичный Метод

Initializes a new instance of the SessionFactoryUtils class.
public SessionFactoryUtils ( ) : System
Результат System

Описание свойств

SESSION_SYNCHRONIZATION_ORDER публичное статическое свойство

The ordering value for synchronizaiton this session resources. Set to be lower than ADO.NET synchronization.
public static int SESSION_SYNCHRONIZATION_ORDER
Результат int