/// <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, 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); }
public void NewTransactionBeforeUsingStatelessSession() { ISessionManager manager = container.Resolve <ISessionManager>(); ITransactionManager tmanager = container.Resolve <ITransactionManager>(); ITransaction transaction = tmanager.CreateTransaction( TransactionMode.Requires, IsolationMode.Serializable); transaction.Begin(); IStatelessSession session = manager.OpenStatelessSession(); Assert.IsNotNull(session); Assert.IsNotNull(session.Transaction); transaction.Commit(); // TODO: Assert transaction was committed // Assert.IsTrue(session.Transaction.WasCommitted); // Assert.IsTrue(session.IsConnected); session.Dispose(); Assert.IsTrue(container.Resolve <ISessionStore>().IsCurrentActivityEmptyFor(Constants.DefaultAlias)); }
public UnregisterEnlistment(ILogger logger, Action callback, ITransaction transaction) { this.callback = callback; id = transaction.Inner.TransactionInformation.LocalIdentifier; logger.Info("Enlisted to undertake " + id); this.logger = logger; }
/// <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); }
public void NewTransactionAfterUsingStatelessSession() { ISessionManager manager = container.Resolve <ISessionManager>(); IStatelessSession session1 = manager.OpenStatelessSession(); ITransactionManager tmanager = container.Resolve <ITransactionManager>(); ITransaction transaction = tmanager.CreateTransaction( TransactionMode.Requires, IsolationMode.Serializable); transaction.Begin(); // Nested using (IStatelessSession session2 = manager.OpenStatelessSession()) { Assert.IsNotNull(session2); Assert.IsNotNull(session1); Assert.IsNotNull(session1.Transaction, "After requesting compatible session, first session is enlisted in transaction too."); Assert.IsTrue(session1.Transaction.IsActive, "After requesting compatible session, first session is enlisted in transaction too."); using (ISession session3 = manager.OpenSession()) { Assert.IsNotNull(session3); Assert.IsNotNull(session3.Transaction); Assert.IsTrue(session3.Transaction.IsActive); } StatelessSessionDelegate delagate1 = (StatelessSessionDelegate)session1; StatelessSessionDelegate delagate2 = (StatelessSessionDelegate)session2; Assert.AreSame(delagate1.InnerSession, delagate2.InnerSession); } transaction.Commit(); // TODO: Assert transaction was committed // Assert.IsTrue(session1.Transaction.WasCommitted); Assert.IsTrue(session1.IsConnected); session1.Dispose(); Assert.IsTrue(container.Resolve <ISessionStore>().IsCurrentActivityEmptyFor(Constants.DefaultAlias)); }
/// <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); }
public void SecondDatabaseStatelessSessionEnlistedOnlyOnceInActualTransaction() { ISessionManager manager = container.Resolve <ISessionManager>(); ITransactionManager tmanager = container.Resolve <ITransactionManager>(); ITransaction transaction = tmanager.CreateTransaction( TransactionMode.Requires, IsolationMode.Serializable); transaction.Begin(); // open connection to first database and enlist session in running transaction IStatelessSession session1 = manager.OpenStatelessSession(); // open connection to second database and enlist session in running transaction using (IStatelessSession session2 = manager.OpenStatelessSession("db2")) { Assert.IsNotNull(session2); Assert.IsNotNull(session2.Transaction); } // "real" NH session2 was not disposed because its in active transaction // request compatible session for db2 --> we must get existing NH session to db2 which should be already enlisted in active transaction using (IStatelessSession session3 = manager.OpenStatelessSession("db2")) { Assert.IsNotNull(session3); Assert.IsTrue(session3.Transaction.IsActive); } transaction.Commit(); // TODO: Assert transaction was committed // Assert.IsTrue(session1.Transaction.WasCommitted); // Assert.IsTrue(session1.IsConnected); session1.Dispose(); Assert.IsTrue(container.Resolve <ISessionStore>().IsCurrentActivityEmptyFor(Constants.DefaultAlias)); }
/// <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> /// 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; }
/// <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; }