コード例 #1
0
        private static void CloseStorage(string dataSourceKey)
        {
            if (string.IsNullOrWhiteSpace(dataSourceKey))
            {
                return;
            }

            ISessionStorage storage = null;

            if (SessionStorages.TryGetValue(dataSourceKey, out storage) && storage != null)
            {
                //Get the current session from the storage object
                ISession session = storage.Session;
                if (session != null)
                {
                    if (session.IsOpen && session.Transaction != null && session.IsConnected && session.Transaction.IsActive &&
                        !session.Transaction.WasCommitted && !session.Transaction.WasRolledBack)
                    {
                        session.Transaction.Rollback();
                        session.Close();
                    }

                    session.Dispose();
                    storage = null;
                }
                SessionStorages.Remove(dataSourceKey);
            }
        }
コード例 #2
0
        /// <summary>
        /// NB: If your tables were created on the fly, then you'll have to pass in the entity name to get the right table.
        /// And, If you must pass in 'autoPersistenceModel', you should only pass in a dynamically created mapping file.
        /// <para>NB: The returned session only Flushes when you commit. You can always change to .FlushMode to your taste.</para>
        /// </summary>
        /// <param name="institutionCode"></param>
        /// <param name="dbConnection"></param>
        /// <param name="doFreshSession"></param>
        /// <returns></returns>
        public static ISession GetSession(string institutionCode = "", IDbConnection dbConnection = null, bool doFreshSession = false)
        {
            bool            isWebSession = IsWeb();
            string          sessionKey   = GetSessionKey(institutionCode, isWebSession);
            ISessionStorage storage      = null;

            if (!doFreshSession)
            {
                SessionStorages.TryGetValue(sessionKey, out storage);
            }
            if (storage == null)
            {
                if (isWebSession)
                {
                    storage = new WebSessionStorage {
                        InstitutionCode = institutionCode
                    };
                }
                else
                {
                    storage = new NonWebSessionStorage();
                }
            }

            //Get the current session from the storage object
            ISession session = doFreshSession ? null : storage.Session;

            //Start a new session if no current session exists
            if (session == null || !session.IsOpen)
            {
                ISessionFactory fac;
                string          instCode = sessionKey.Split('_')[0]; // will be 'Utilities.INST_DEFAULT_CODE' if 'institutionCode' is null or empty
                if (SessionFactories.TryGetValue(instCode, out fac) && fac != null)
                {
                    //Do what now? Nothing.
                }
                else
                {
                    fac = BuildFactory(instCode, sessionKey);
                }

                //Apply the interceptor if any was registered and open the session
                if (dbConnection == null)
                {
                    session = fac.OpenSession();
                }
                else
                {
                    session = fac.OpenSession(dbConnection);
                }
            }

            if (session != null)
            {
                session.EnableFilter(Utilities.InstitutionFilterName)
                .SetParameter(Utilities.InstitutionCodeQueryParamName, institutionCode)
                .SetParameter(Utilities.SoftDeleteParamName, true);
                session.FlushMode = FlushMode.Commit;
                //Begin a transaction
                if (!session.IsConnected || session.Transaction == null || !session.Transaction.IsActive || session.Transaction.WasCommitted || session.Transaction.WasRolledBack) //if (storage is WebSessionStorage)
                {
                    session.BeginTransaction();
                }
            }
            if (!doFreshSession)
            {
                //Update the storage with the new session
                storage.Session             = session;
                SessionStorages[sessionKey] = storage;
            }
            //if (isWebSession)
            //{
            //    SessionStorages[sessionKey] = (WebSessionStorage)storage;
            //}

            return(session);
        }