Beispiel #1
0
        /// <summary>
        /// Gets the context in the thread
        /// </summary>
        /// <returns>the NHContext</returns>
        protected static NHContext GetNHContext()
        {
            NHContext context = HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] as NHContext;

            string contextString = "null";

            if (context != null)
            {
                contextString = context.ToString();
            }

            log.Debug("NHWebContext value:" + contextString);
            if (context == null)
            {
                log.Debug("No context found, creating a new one on thread " + AppDomain.GetCurrentThreadId().ToString());
                context = new NHWebContext();
                SetNHContext(context);
            }
            else
            {
                log.Debug("Context found on thread " + AppDomain.GetCurrentThreadId().ToString());
            }

            return(context);
        }
Beispiel #2
0
        /// <summary>
        /// Gets the context in the thread
        /// </summary>
        /// <returns>the NHContext</returns>
        protected static NHContext GetNHContext()
        {
            NHContext context = CallContext.GetData(NH_CONTEXT_CALL_THREAD_CONTEXT_KEY) as NHContext;

            if (context == null)
            {
                context = new NHThreadContext();
                SetNHContext(context);
            }

            return(context);
        }
Beispiel #3
0
        /// <summary>
        /// Builds an NHSessionWrapper and NHContext objects and associates them
        /// with the current thread.
        /// This method will also start an nhibernate transaction
        /// </summary>
        /// <remarks>
        /// The nhibernate configuration will be read from the current assembly (Hexacta.Framework.NH.Util)
        /// </remarks>
        public static void PrepareNHSessionWithThread(NHContext context)
        {
            try {
                NHSessionWrapper wrapper = context.NHSessionWraper;

                wrapper.OpenSession();
                //start the NH transaction
                wrapper.BeginTransaction();
            } catch (Exception) {
                log.Fatal("add error message");                 //TODO: Add error message
                throw;
            }
        }
 /// <summary>
 /// Sets the NHcontext
 /// </summary>
 /// <param name="context">the context</param>
 protected static void SetNHContext(NHContext context)
 {
     log.Debug("Setting up NHWebContext into CallContext");
     if (HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] == null) {
         log.Debug("NHWebContext was null. Adding context");
         //in strange cases the key is set but the value is null
         if (HttpContext.Current.Items.Contains(NH_CONTEXT_CALL_CONTEXT_KEY) && HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] == null) {
             log.Debug("NHWebContext was null but key was found in context");
             HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] = context;
         }
         HttpContext.Current.Items.Add(NH_CONTEXT_CALL_CONTEXT_KEY, context);
     } else {
         log.Debug("NHWebContext was not null. Replacing older context");
         HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] = context;
     }
 }
Beispiel #5
0
 /// <summary>
 /// Sets the NHcontext
 /// </summary>
 /// <param name="context">the context</param>
 protected static void SetNHContext(NHContext context)
 {
     log.Debug("Setting up NHWebContext into CallContext");
     if (HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] == null)
     {
         log.Debug("NHWebContext was null. Adding context");
         //in strange cases the key is set but the value is null
         if (HttpContext.Current.Items.Contains(NH_CONTEXT_CALL_CONTEXT_KEY) && HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] == null)
         {
             log.Debug("NHWebContext was null but key was found in context");
             HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] = context;
         }
         HttpContext.Current.Items.Add(NH_CONTEXT_CALL_CONTEXT_KEY, context);
     }
     else
     {
         log.Debug("NHWebContext was not null. Replacing older context");
         HttpContext.Current.Items[NH_CONTEXT_CALL_CONTEXT_KEY] = context;
     }
 }
Beispiel #6
0
        /// <summary>
        /// Releases all nhibernate resources allocated to the current thread when
        /// an exception has occured.
        /// This method is similar to ReleaseNHSessionFromThread except that it will
        /// not try to commit the transaction
        /// </summary>
        /// <param name="context">the context to use.</param>
        /// <param name="clearContex">wheather to realse the nhibernate context.
        /// If a thread is going to be reused leave this option as false
        /// </param>
        public static void ReleaseNHSessionFromThreadException(NHContext context, bool clearContex)
        {
            log.Debug("Method:" + "ReleaseNHSessionFromThreadException");

            NHSessionWrapper wrapper = context.NHSessionWraper;

            if (wrapper != null && !wrapper.Transaction.WasRolledBack)
            {
                try {
                    wrapper.RollbackTransaction();
                } catch (HibernateException hEx) {
                    //log.Fatal(SR.UnrecoverableNHibernateCommitException, hEx);
                    throw hEx;
                } finally {
                    try {
                        if (!wrapper.IsSessionNull)
                        {
                            wrapper.CloseSession();
                        }
                    } catch {
                        //TODO: log exception
                        throw;
                    }

                    SyncContext.Current.ReleaseMonitored();
                    //ApplicationContext.Current.Release();
                }
            }

            if (clearContex)
            {
                context.ClearContext();
                SyncContext.Current.ClearContext();
                //ApplicationContext.Current.ClearContext();
            }
        }
Beispiel #7
0
        /// <summary>
        /// Releases all nhibernate resources allocated to the current thread.
        /// It will also commit any started transactions and close the connection as necesary
        /// </summary>
        /// <param name="context">the context to use.</param>
        /// <param name="clearContex">wheather to realse the nhibernate context.
        /// If a thread is going to be reused leave this option as false
        /// </param>
        public static void ReleaseNHSessionFromThread(NHContext context, bool clearContex)
        {
            log.Debug("Method:" + "ReleaseNHSessionFromThread");
            NHSessionWrapper wrapper = context.NHSessionWraper;

            object threadId = CallContext.GetData("threadID");

            if (threadId != null)
            {
                log.Debug(threadId.ToString());
            }
            else
            {
                log.Debug("No thread id found on CallContext");
            }
            log.Debug("Current ThreadID=" + AppDomain.GetCurrentThreadId().ToString());

            if (wrapper != null)
            {
                try {
                    log.Debug("Try: Checking seesion and transaction for commit");
                    if (!wrapper.IsSessionNull)
                    {
                        if (!wrapper.Transaction.WasCommitted)
                        {
                            log.Debug("Transaction not commited; about to commit");
                            wrapper.CommitTransaction();
                        }
                    }
                } catch (Exception ex) {
                    //log.Fatal(SR.UnrecoverableNHibernateCommitException, ex);
                    try {
                        log.Debug("Try: about to roll back transaction");
                        wrapper.RollbackTransaction();
                    } catch (HibernateException hEx) {
                        //log.Fatal(SR.UnrecoverableNHibernateRollbackException, hEx);
                        throw;
                    }
                    throw;
                } finally {
                    try {
                        if (!wrapper.IsSessionNull)
                        {
                            log.Debug("Try: about to close session");
                            wrapper.CloseSession();
                        }
                    } catch {
                        log.Fatal("Error closing session");                         //TODO: Add error message
                        throw;
                    }
                    SyncContext.Current.ReleaseMonitored();
                    //ApplicationContext.Current.Release();
                }
            }

            if (clearContex)
            {
                log.Debug("Clearing context...");
                context.ClearContext();
                SyncContext.Current.ClearContext();
                //ApplicationContext.Current.ClearContext();
            }
        }
Beispiel #8
0
 /// <summary>
 /// Sets the NHcontext
 /// </summary>
 /// <param name="context">the context</param>
 protected static void SetNHContext(NHContext context)
 {
     log.Debug("Setting up NHThreadContext into CallContext");
     CallContext.SetData(NH_CONTEXT_CALL_THREAD_CONTEXT_KEY, context);
 }