コード例 #1
0
        private OpenSessionInViewSection GetOpenSessionInViewSection()
        {
            OpenSessionInViewSection openSessionInViewSection = ConfigurationManager
                                                                .GetSection("nhibernateSettings") as OpenSessionInViewSection;

            Check.Ensure(openSessionInViewSection != null,
                         "The nhibernateSettings section was not found with ConfigurationManager.");
            return(openSessionInViewSection);
        }
コード例 #2
0
        /// <summary>
        /// Commits and closes the NHibernate session provided by the supplied <see cref="NHibernateSessionManager"/>.
        /// Assumes a transaction was begun at the beginning of the request; but a transaction or session does
        /// not *have* to be opened for this to operate successfully.
        /// </summary>
        private void CommitAndCloseSession(object sender, EventArgs e)
        {
            OpenSessionInViewSection openSessionInViewSection = GetOpenSessionInViewSection();

            try
            {
                // Commit every session factory that's holding a transactional session
                foreach (SessionFactoryElement sessionFactorySettings in openSessionInViewSection.SessionFactories)
                {
                    if (sessionFactorySettings.IsTransactional)
                    {
                        NHibernateSessionManager.Instance.CommitTransactionOn(sessionFactorySettings.FactoryConfigPath);
                    }
                }
            }
            finally
            {
                // No matter what happens, make sure all the sessions get closed
                foreach (SessionFactoryElement sessionFactorySettings in openSessionInViewSection.SessionFactories)
                {
                    NHibernateSessionManager.Instance.CloseSessionOn(sessionFactorySettings.FactoryConfigPath);
                }
            }
        }