private ISession CreateSession(String alias)
        {
            ISessionFactory sessionFactory = factoryResolver.GetSessionFactory(alias);

            if (sessionFactory == null)
            {
                throw new FacilityException("No ISessionFactory implementation " +
                                            "associated with the given alias: " + alias);
            }

            ISession session;

            string aliasedInterceptorId = string.Format(InterceptorFormatString, alias);

            if (kernel.HasComponent(aliasedInterceptorId))
            {
                IInterceptor interceptor = kernel.Resolve <IInterceptor>(aliasedInterceptorId);

                session = sessionFactory.OpenSession(interceptor);
            }
            else if (kernel.HasComponent(InterceptorName))
            {
                IInterceptor interceptor = kernel.Resolve <IInterceptor>(InterceptorName);

                session = sessionFactory.OpenSession(interceptor);
            }
            else
            {
                session = sessionFactory.OpenSession();
            }

            session.FlushMode = defaultFlushMode;

            return(session);
        }
        public void WhenBeginTransactionFailsSessionIsRemovedFromSessionStore()
        {
            using (mockRepository.Record())
            {
                Expect.Call(kernel.Resolve <ITransactionManager>()).Return(transactionManager);
                Expect.Call(transactionManager.CurrentTransaction).Return(transaction);
                Expect.Call(factoryResolver.GetSessionFactory(Alias)).Return(sessionFactory);
                Expect.Call(kernel.HasComponent(string.Format(InterceptorFormatString, Alias))).Return(false);
                Expect.Call(kernel.HasComponent(InterceptorName)).Return(false).Repeat.Any();
                Expect.Call(sessionFactory.OpenSession()).Return(session);
                session.FlushMode = sessionManager.DefaultFlushMode;
                Expect.Call(transaction.Context).Return(contextDictionary).Repeat.Any();
                Expect.Call(transaction.IsolationMode).Return(DefaultIsolationMode).Repeat.Any();
                Expect.Call(session.BeginTransaction(DefaultIsolationLevel)).Throw(new Exception());
            }

            using (mockRepository.Playback())
            {
                try
                {
                    sessionManager.OpenSession(Alias);
                    Assert.Fail("DbException not thrown");
                }
                catch (Exception)
                {
                    //ignore
                    //Console.WriteLine(ex.ToString());
                }
                Assert.IsNull(sessionStore.FindCompatibleSession(Alias),
                              "The sessionStore shouldn't contain compatible session if the session creation fails");
            }
        }
Exemplo n.º 3
0
        private ISession CreateSession(String alias)
        {
            ISessionFactory sessionFactory = factoryResolver.GetSessionFactory(alias);

            if (sessionFactory == null)
            {
                throw new FacilityException("No ISessionFactory implementation " +
                                            "associated with the given alias: " + alias);
            }

            ISession session;

            string aliasedInterceptorId = string.Format("nhibernate.session.interceptor.{0}", alias);

            if (kernel.HasComponent(aliasedInterceptorId))
            {
                IInterceptor interceptor = (IInterceptor)kernel[aliasedInterceptorId];

                return(sessionFactory.OpenSession(interceptor));
            }
            else if (kernel.HasComponent("nhibernate.session.interceptor"))
            {
                IInterceptor interceptor = (IInterceptor)kernel["nhibernate.session.interceptor"];

                session = sessionFactory.OpenSession(interceptor);
            }
            else
            {
                session = sessionFactory.OpenSession();
            }

            session.FlushMode = defaultFlushMode;

            return(session);
        }