/// <summary> /// Returns a valid opened and connected IStatelessSession instance /// for the given connection alias. /// </summary> /// <param name="alias"></param> /// <returns></returns> public IStatelessSession OpenStatelessSession(String alias) { if (alias == null) { throw new ArgumentNullException("alias"); } ITransaction transaction = ObtainCurrentTransaction(); StatelessSessionDelegate wrapped = sessionStore.FindCompatibleStatelessSession(alias); if (wrapped == null || (transaction != null && !wrapped.Transaction.IsActive)) { IStatelessSession session = CreateStatelessSession(alias); wrapped = WrapSession(transaction == null, session); EnlistIfNecessary(true, transaction, wrapped); sessionStore.Store(alias, wrapped); } else { EnlistIfNecessary(false, transaction, wrapped); wrapped = WrapSession(false, wrapped.InnerSession); } 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, StatelessSessionDelegate session) { if (transaction == null) { return(false); } var list = (IList <IStatelessSession>)transaction.Context["nh.statelessSession.enlisted"]; bool shouldEnlist; if (list == null) { list = new List <IStatelessSession>(); shouldEnlist = true; } else { shouldEnlist = true; foreach (IStatelessSession sess in list) { if (StatelessSessionDelegate.AreEqual(session, sess)) { shouldEnlist = false; break; } } } if (shouldEnlist) { if (session.Transaction == null || !session.Transaction.IsActive) { transaction.Context["nh.statelessSession.enlisted"] = list; IsolationLevel level = TranslateIsolationLevel(transaction.IsolationMode); transaction.Enlist(new ResourceAdapter(session.BeginTransaction(level), transaction.IsAmbient)); list.Add(session); } if (weAreSessionOwner) { transaction.RegisterSynchronization(new StatelessSessionDisposeSynchronization(session)); } } return(true); }
/// <summary> /// Returns <see langword="true"/> if the supplied stateless sessions are equal, <see langword="false"/> otherwise. /// </summary> /// <param name="left">The left.</param> /// <param name="right">The right.</param> /// <returns></returns> public static bool AreEqual(IStatelessSession left, IStatelessSession right) { StatelessSessionDelegate ssdLeft = left as StatelessSessionDelegate; StatelessSessionDelegate ssdRight = right as StatelessSessionDelegate; if (ssdLeft != null && ssdRight != null) { return(Object.ReferenceEquals(ssdLeft._innerSession, ssdRight._innerSession)); } else { throw new NotSupportedException("AreEqual: left is " + left.GetType().Name + " and right is " + right.GetType().Name); } }
/// <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, StatelessSessionDelegate session) { if (transaction == null) { return(false); } if (weAreSessionOwner && session.Transaction.IsActive) { var ue = new UnregisterEnlistment(Logger, session.UnregisterFromStore, transaction); transaction.Inner.EnlistVolatile(ue, EnlistmentOptions.EnlistDuringPrepareRequired); } return(true); }