Spring.Data.NHibernate.SessionFactoryUtils.GetSession C# (CSharp) Method

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
        public static ISession GetSession(
            ISessionFactory sessionFactory, IInterceptor entityInterceptor,
            IAdoExceptionTranslator adoExceptionTranslator)
        {
            try
            {
                return GetSession(sessionFactory, entityInterceptor, adoExceptionTranslator, true);
            }
            catch (HibernateException ex)
            {
                throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
            }
        }   

Same methods

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

Usage Example

示例#1
0
        public object DoInTransaction(ITransactionStatus status)
        {
            SessionHolder holder = (SessionHolder)TransactionSynchronizationManager.GetResource(sf);

            Assert.IsNotNull(holder, "Has thread session");
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
            Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
            tt.PropagationBehavior = TransactionPropagation.NotSupported;
            tt.Execute(new NotSupportedTxCallback(sf));

            Assert.IsTrue(holder.Session == SessionFactoryUtils.GetSession(sf, false), "Same thread session as before");
            Assert.IsFalse(TransactionSynchronizationManager.CurrentTransactionReadOnly);
            Assert.IsTrue(TransactionSynchronizationManager.ActualTransactionActive);
            return(null);
        }