1 db2 nhibernate transactions ibm-midrange
考虑到以下代码块,为什么调用HQL工作但调用delete()不起作用?作为背景,我使用NHibernate而不是IBM.Data.DB2.Iseries驱动程序.来发现,AS400上的日志已经关闭,所以我无法使用交易.我不是AS400管理员或者对它有任何了解所以我不知道是否关闭了日记功能(不打开事务)是否导致了这个问题.如果我正在调用Delete()或其他NHibernate函数,我是否绝对需要能够打开事务?
//This Does not work - no error and no deletes
public static void Delete(Object Entity)
{
using (ISession session = _sessionFactory.OpenSession())
{
//using(ITransaction tx = session.BeginTransaction())
//{
session.Delete(Entity);
//tx.Commit();
session.Close();
//}
}
}
//This does work
public static void Delete(Object Entity)
{
using (ISession session = _sessionFactory.OpenSession())
{
//commented out transaction control because throws error because
//journaling is not turned on the AS400
//using(ITransaction tx = session.BeginTransaction())
//{
session.CreateQuery("delete MyDAO p where p.MyDAOID = :MyDAOID").SetString("MyDAOID", ((MyDAO)Entity).MyDAOID.ToString()).ExecuteUpdate();
//}
}
}
Run Code Online (Sandbox Code Playgroud)
session.Flush()删除后尝试调用,但在关闭会话之前.而且您不需要显式关闭会话:
using (var session = ...)
{
entity.Delete();
session.Flush();
}
Run Code Online (Sandbox Code Playgroud)