コード例 #1
0
 /// <summary>
 /// When a transaction start all session must be disconnected from the current connection
 /// and reconnect to a different connection.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private static void OnTransactionStarted(Object sender, EventArgs e)
 {
     //if we are in a nested transaction we do not need to do nothing, we are working simply
     //with more transaction level.
     if (GlobalTransactionManager.TransactionsCount > 1)
     {
         return;
     }
     //This is the first time that a transaction start, we need to change all connection with those
     //of the DataAccess layer.
     IterateThroughAllOpenSessionInContext(sd =>
     {
         DataAccess.ConnectionData data = DataAccess.GetActualConnectionData(sd.connectionName);
         sd.Session.Disconnect();
         sd.Session.Reconnect(data.Connection);
     });
 }
コード例 #2
0
        public static ISession GetSessionFor(String configFileName)
        {
            Object obj = CurrentContext.GetData(GetContextSessionKeyForConfigFileName(configFileName));

            if (null != obj)
            {
                //We have a session on the stack, but, it is valid?
                ISession contextSession = ((SessionData)obj).Session;
                if (contextSession.IsOpen)
                {
                    return(contextSession);
                }
                //Session is not valid, remove from the context.
                CurrentContext.ReleaseData(GetContextSessionKeyForConfigFileName(configFileName));
            }
            //If we reach here we have no context connection or the context connection was disposed.
            NhibConfigData configData = GetOrCreateConfigData(configFileName);

            ISession session = null;

            if (GlobalTransactionManager.IsInTransaction)
            {
                DataAccess.ConnectionData data = DataAccess.GetActualConnectionData(configData.ConnectionName);
                session = configData.SessionFactory.OpenSession(data.Connection);
            }
            else
            {
                session = configData.SessionFactory.OpenSession();
            }
            CurrentContext.SetData(
                GetContextSessionKeyForConfigFileName(configFileName),
                new SessionData()
            {
                Session = session, connectionName = configData.ConnectionName
            });
            return(session);
        }