Example #1
0
        public void RunWorker(BackgroundWorker worker, DoWorkEventArgs args)
        {
            var getProgress = args.Argument as Func <ProgressState>
                              ?? new Func <ProgressState>(() => ProgressState.Empty);

            CurrentProgress = getProgress() ?? ProgressState.Empty;
            ImportStarted(this, EventArgs.Empty);
            var type           = Type.GetType(DataContextObject.SelectedDataType.Target.ClrType);
            var sessionFactory = DataContextObject.ShellContext.SessionFactoryHolder.GetSessionFactory();

            CurrentProgress = new ProgressState(0, DataContextObject.TargetsToImport.Count);
            using (NHibernate.ISession session = sessionFactory.OpenSession())
            {
                var current   = 0;
                var total     = DataContextObject.TargetsToImport.Count;
                int batchSize = Settings.Default.BatchSize;
                using (var tx = session.BeginTransaction())
                {
                    while (current < total && !args.Cancel && !Cancelled)
                    {
                        if (worker.CancellationPending)
                        {
                            args.Cancel = true;
                        }
                        else
                        {
                            var target = DataContextObject.TargetsToImport[current];
                            (target as ContentPartRecord).ContentItemRecord = DataContextObject.CurrentContentItem;
                            try
                            {
                                session.Save(DataContextObject.TargetsToImport[current]);
                            }
                            catch (Exception ex)
                            {
                                GuardError(current, ex);
                            }
                            finally
                            {
                                if (current % batchSize == 0)
                                { //20, same as the ADO batch size
                                    session.Flush();
                                    session.Clear();
                                }
                                current++;
                                var progress = new ProgressState(current, total);
                                worker.ReportProgress((int)Math.Floor(progress.Ratio * 100d), progress);
                                if (current % 1000 == 0)
                                {
                                    Application.Current.DoEvents();
                                }
                            }
                        }
                    }
                    session.Flush();
                    session.Clear();
                    tx.Commit();
                }
                session.Close();
            }
        }
Example #2
0
 /// <summary>
 /// 当用户断开请求时用来关闭用户请求的Session的连接
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 protected void Session_End(object sender, EventArgs e)
 {
     NHibernate.ISession session = SessionFactory.GetSession();
     if (session != null)
     {
         session.Close();
     }
 }
        private void JapaneseStudyForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            var eventLocation = this.GetType().Name + "." + MethodBase.GetCurrentMethod().Name;

            var dialogMessage = "Save before exiting?";

            if (_changesOccurred)
            {
                var result = MessageBox.Show(dialogMessage, "Save?", MessageBoxButtons.YesNoCancel);

                _logger.Info("Result is " + result);

                if (result == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
                else
                {
                    if (result == DialogResult.Yes)
                    {
                        using (var tx = _dbSession.BeginTransaction())
                        {
                            foreach (var oneWord in _wordsToStudy)
                            {
                                _dbSession.Save(oneWord);
                            }
                            tx.Commit();
                        }
                    }

                    _dbSession.Close();
                    _dbConnection.Close();

                    _logger.CloseSection(eventLocation);
                    _logger.CloseSection(_location);
                }
            }
            else
            {
                _dbSession.Close();
                _dbConnection.Close();

                _logger.CloseSection(eventLocation);
                _logger.CloseSection(_location);
            }
        }
Example #4
0
        protected void Application_EndRequest(object sender, EventArgs e)
        {
            NHibernate.ISession session = ((NHibernate.ISession)Context.Items["hibernateSession"]);

            if (session != null && session.IsOpen)
            {
                session.Close();
            }
        }
Example #5
0
        public async Task Invoke(HttpContext httpContext, NHibernate.ISession session)
        {
            await _next(httpContext);

            if (session != null && session.IsOpen)
            {
                session.Close();
            }
        }
Example #6
0
 public void ClearSession()
 {
     ClearData();
     if (session != null && session.IsOpen)
     {
         session.Close();
         session.Dispose();
         session = null;
     }
 }
Example #7
0
        public void ClearSession()
        {
            ClearData();
            if (session != null && session.IsOpen)
            {
                dataGridView.Rows.Clear();

                session.Close();
                session.Dispose();
                session = null;
            }
        }
Example #8
0
        private void context_EndRequest(object sender, EventArgs e)
        {
            System.Web.HttpApplication aHttpApplication = (System.Web.HttpApplication)sender;
            System.Web.HttpContext     aHttpContext     = aHttpApplication.Context;

            NHibernate.ISession aNHibernateSession = NHibernateHelper.GetCurrentSession(aHttpContext);

            if (aNHibernateSession != null)
            {
                aNHibernateSession.Flush();
                aNHibernateSession.Close();
            }

            NHibernateHelper.DisposeCurrentSession(aHttpContext);
        }
        IMethodReturn IInterceptionBehavior.Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
        {
            IMethodReturn retour = null;

            TransactionalAttribute transactional = null;

            object[] atts = input.MethodBase.GetCustomAttributes(typeof(TransactionalAttribute), false);
            if (atts.Count() > 0)
            {
                transactional = (TransactionalAttribute)atts[0];
            }

            if (Utility.MyNHibernateHelper.SessionManager.Configuration == null)
            {
                SessionManager.Configuration = TransactionInterceptorConfiguration.Configuration;
                TransactionInterceptorConfiguration.SessionManager = new SessionManager();
            }

            if (NHibernate.Context.CurrentSessionContext.HasBind(SessionManager.SessionFactory) == false)
            {
                NHibernate.Context.CurrentSessionContext.Bind(SessionManager.SessionFactory.OpenSession());
            }

            NHibernate.ISession     session     = null;
            NHibernate.ITransaction transaction = null;
            if (transactional != null)
            {
                session = TransactionInterceptorConfiguration.SessionManager.CurrentSession;
                if (transactional.OpenTransaction)
                {
                    transaction = session.BeginTransaction();
                }
            }

            try
            {
                retour = getNext()(input, getNext);
                if (transaction != null && transactional.RollBack == true)
                {
                    transaction.Rollback();
                    //if (typeof(retour.Exception) == typeof(NHibernate.Exceptions.GenericADOException)) retour.Exception = null;
                }
                else if (retour.Exception == null && transaction != null)
                {
                    transaction.Commit();
                }
                else if (retour.Exception != null)
                {
                    throw retour.Exception;
                }
            }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }
                else
                {
                    throw ex;
                }
            }
            finally
            {
                if (transaction != null)
                {
                    transaction.Dispose();
                }
                if (session != null)
                {
                    session.Close();
                    session.Dispose();
                    NHibernate.Context.CurrentSessionContext.Unbind(SessionManager.SessionFactory);
                }
            }

            return(retour);
        }