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); }
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(); }
public void Store(String alias, SessionDelegate session) { Stack stack = GetStackFor(alias); stack.Push(session); session.SessionStoreCookie = stack; }
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; }