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".
精彩评论