예제 #1
0
        public ISession OpenSession(String alias)
        {
            if (alias == null)
            {
                throw new ArgumentNullException("alias");
            }

            ITransaction transaction = ObtainCurrentTransaction();

            bool weAreSessionOwner = false;

            SessionDelegate wrapped = sessionStore.FindCompatibleSession(alias);

            ISession session;

            if (wrapped == null)
            {
                session = CreateSession(alias); weAreSessionOwner = true;

                wrapped = WrapSession(transaction != null, session);

                sessionStore.Store(alias, wrapped);

                EnlistIfNecessary(weAreSessionOwner, transaction, wrapped);
            }
            else
            {
                EnlistIfNecessary(weAreSessionOwner, transaction, wrapped);
                wrapped = WrapSession(true, wrapped.InnerSession);
            }

            return(wrapped);
        }
예제 #2
0
        public void Remove(SessionDelegate session)
        {
            Stack stack = (Stack)session.SessionStoreCookie;

            if (stack == null)
            {
                throw new InvalidProgramException("AbstractSessionStore.Remove called " +
                                                  "with no cookie - no pun intended");
            }

            if (stack.Count == 0)
            {
                throw new InvalidProgramException("AbstractSessionStore.Remove called " +
                                                  "for an empty stack");
            }

            ISession current = stack.Peek() as ISession;

            if (session != current)
            {
                throw new InvalidProgramException("AbstractSessionStore.Remove tried to " +
                                                  "remove a session which is not on the top or not in the stack at all");
            }

            stack.Pop();
        }
예제 #3
0
        public void Store(String alias, SessionDelegate session)
        {
            Stack stack = GetStackFor(alias);

            stack.Push(session);

            session.SessionStoreCookie = stack;
        }
예제 #4
0
        protected bool EnlistIfNecessary(bool weAreSessionOwner,
                                         ITransaction transaction,
                                         SessionDelegate session)
        {
            if (transaction == null)
            {
                return(false);
            }

            IList list = (IList)transaction.Context["nh.session.enlisted"];

            bool shouldEnlist;

            if (list == null)
            {
                list = new ArrayList();

                transaction.Context["nh.session.enlisted"] = list;

                shouldEnlist = true;
            }
            else
            {
                shouldEnlist = true;

                foreach (ISession sess in list)
                {
                    if (SessionDelegate.AreEqual(session, sess))
                    {
                        shouldEnlist = false;
                        break;
                    }
                }
            }

            if (shouldEnlist)
            {
                // TODO: propagate IsolationLevel, expose as transaction property

                transaction.Enlist(new ResourceAdapter(session.BeginTransaction()));

                list.Add(session);

                if (weAreSessionOwner)
                {
                    transaction.RegisterSynchronization(
                        new SessionDisposeSynchronization(session));
                }
            }

            return(true);
        }
        public static bool AreEqual(ISession left, ISession right)
        {
            SessionDelegate sdLeft  = left as SessionDelegate;
            SessionDelegate sdRight = right as SessionDelegate;

            if (sdLeft != null && sdRight != null)
            {
                return(Object.ReferenceEquals(sdLeft.inner, sdRight.inner));
            }
            else
            {
                throw new NotSupportedException("AreEqual: left is " +
                                                left.GetType().Name + " and right is " + right.GetType().Name);
            }
        }
 public SessionDisposeSynchronization(SessionDelegate session)
 {
     this.session = session;
 }