【发布时间】:2015-02-26 19:21:27
【问题描述】:
在会话期间应该如何处理 NHibernate 异常?网上有很多例子:
http://nhibernate.info/doc/nh/en/index.html#manipulatingdata-exceptions https://svn.code.sf.net/p/nhibernate/code/trunk/nhibernate/src/NHibernate/ISession.cs
还有很多很多 StackOwerflow 线程都建议采用类似于此的方法:
using (ISession session = factory.OpenSession())
using (ITransaction tx = session.BeginTransaction())
{
try
{
// do some work
...
tx.Commit();
}
catch (Exception e)
{
if (tx != null) tx.Rollback();
throw;
}
}
但是如果发生错误并且在第一行代码中抛出异常(当您打开会话时)怎么办?这些例子都没有掩盖它!
我的一所大学建议采用这种方法:
ITransaction transaction = null;
try
{
using (ISession session = databaseFacade.OpenSession())
{
transaction = session.BeginTransaction();
//do some work
...
transaction.Commit();
}
}
catch (Exception ex)
{
if (transaction != null)
transaction.Rollback();
throw new Exception(ex.Message);
}
【问题讨论】:
标签: c# nhibernate fluent-nhibernate