开发者

Which exceptions should lead to closing of Nhibernate sessions?

开发者 https://www.devze.com 2023-03-11 11:22 出处:网络
I was reading that Nhibernate exceptions lead to invalid session state. So, my question is which exceptions should I handle and close and reopen the session.? And, should I reload all entities?

I was reading that Nhibernate exceptions lead to invalid session state. So, my question is which exceptions should I handle and close and reopen the session.? And, should I reload all entities?

My scenario - I am opening a session in my presenter class for a form. And, I am using transactions like



 using (ITransaction transaction = session.BeginTransaction())
            {
                foreach (var item in records)
                {
                    session.Delete(item);
                }
                transaction.Commit();
           }

so, should I do this?

using (ITransaction transaction = session.BeginTransaction())
            {
                foreach (var item in records)
                {
                    session.Delete(item);
                }
                try
               {
                transaction.Commit();
               }
             catch(Exception ex)
               {
 开发者_Python百科                rollback,
                session.dispose
                session = factor.opensession()
                }

           }


First off, I use the second option all the time. Now to the question, disposing and opening a new session is practically painless so I usually don't mind doing it "if any error occurs".

0

精彩评论

暂无评论...
验证码 换一张
取 消