Example #1
0
        /// <summary>
        /// Enlists if necessary.
        /// </summary>
        /// <param name="weAreSessionOwner">if set to <c>true</c> [we are session owner].</param>
        /// <param name="transaction">The transaction.</param>
        /// <param name="session">The session.</param>
        /// <returns></returns>
        protected bool EnlistIfNecessary(bool weAreSessionOwner,
                                         ITransaction transaction,
                                         SessionDelegate session)
        {
            if (transaction == null)
            {
                Logger.Info("Tx not found. Nothing to do here.");

                return(false);
            }

            Logger.InfoFormat("Enlistment status. Session: {0}. Tx IsActive: {1}. Are we Session Owner: {2}",
                              session.GetSessionImplementation().SessionId, session.Transaction.IsActive, weAreSessionOwner);

            if (weAreSessionOwner && session.Transaction.IsActive)
            {
                Logger.Info("Enlisted Session " + session.GetSessionImplementation().SessionId);

                var ue = new UnregisterEnlistment(Logger, session.UnregisterFromStore, transaction);

                transaction.Inner.EnlistVolatile(ue, EnlistmentOptions.EnlistDuringPrepareRequired);
            }

            return(true);
        }
        /// <summary>
        /// Returns a valid opened and connected ISession instance
        /// for the given connection alias.
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public ISession OpenSession(String alias)
        {
            if (alias == null)
            {
                throw new ArgumentNullException("alias");
            }

            ITransaction transaction = ObtainCurrentTransaction();

            SessionDelegate wrapped = sessionStore.FindCompatibleSession(alias);

            ISession session;

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

                wrapped = WrapSession(transaction != null, session);
                EnlistIfNecessary(true, transaction, wrapped);
                sessionStore.Store(alias, wrapped);
            }
            else
            {
                EnlistIfNecessary(false, transaction, wrapped);
                wrapped = WrapSession(true, wrapped.InnerSession);
            }

            return(wrapped);
        }
Example #3
0
        /// <summary>
        /// Returns a valid opened and connected ISession instance
        /// for the given connection alias.
        /// </summary>
        /// <param name="alias"></param>
        /// <returns></returns>
        public ISession OpenSession(String alias)
        {
            if (alias == null)
            {
                throw new ArgumentNullException("alias");
            }

            ITransaction transaction = ObtainCurrentTransaction();

            SessionDelegate wrapped = sessionStore.FindCompatibleSession(alias);

            if (wrapped == null || (transaction != null && !wrapped.Transaction.IsActive))
            {
                var session = CreateSession(alias);

                Logger.Info("Created Session " + session.GetSessionImplementation().SessionId);

                wrapped = WrapSession(transaction == null, session);
                EnlistIfNecessary(true, transaction, wrapped);

                sessionStore.Store(alias, wrapped);

                Logger.Info("Wrapped Session " + session.GetSessionImplementation().SessionId);
            }
            else
            {
                wrapped = WrapSession(false, wrapped.InnerSession);
                EnlistIfNecessary(false, transaction, wrapped);
            }

            return(wrapped);
        }
        /// <summary>
        /// Enlists if necessary.
        /// </summary>
        /// <param name="weAreSessionOwner">if set to <c>true</c> [we are session owner].</param>
        /// <param name="transaction">The transaction.</param>
        /// <param name="session">The session.</param>
        /// <returns></returns>
        protected bool EnlistIfNecessary(bool weAreSessionOwner,
                                         ITransaction transaction,
                                         SessionDelegate session)
        {
            if (transaction == null)
            {
                return(false);
            }

            var list = (IList <ISession>)transaction.Context["nh.session.enlisted"];

            bool shouldEnlist;

            if (list == null)
            {
                list = new List <ISession>();

                shouldEnlist = true;
            }
            else
            {
                shouldEnlist = true;

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

            if (shouldEnlist)
            {
                if (session.Transaction == null || !session.Transaction.IsActive)
                {
                    transaction.Context["nh.session.enlisted"] = list;

                    IsolationLevel level = TranslateIsolationLevel(transaction.IsolationMode);
                    transaction.Enlist(new ResourceAdapter(session.BeginTransaction(level), transaction.IsAmbient));

                    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);
            }
        }