2

在一个窗口应用程序中,我们使用 nHibernate。我们在更新表(Tag1 或 Tag2)的数据时遇到问题,并且在同一个 ISession 中,我们使用 Oracle 包将表中的数据插入到另一个表(QA 表)中。提交时,Oracle 包在 Tag1/Tag2 表中看不到更改的数据,因此在 QA 表中未更新修改的数据,可能是因为在同一个会话中被调用?

    using (ISession session = iNhibernet.OpenSession())
            {

                using (ITransaction transaction = session.BeginTransaction())
                {

                    try
                    {
                        // Business Entity Saved in Tag1/Tag2 Table
                        session.SaveOrUpdate(l);
                    }
                    catch (Exception ex)
                    {
                        ErrorLogExceptionHandler.ErrorLog(ref ex);
                        throw new Exception("Unable to save data");
                    }
                    // Calling Oracle Package to Compare Tag1 and Tag2 data and inserting data in QA Table.
                    IDbCommand db = ProductionLog.ProductionLogUpdate(l.ProductionlogSeqNo, loadAction) as DbCommand;
                    db.Connection = session.Connection;
                    try
                    {
                        db.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        ErrorLogExceptionHandler.ErrorLog(ref ex);
                        throw new Exception("Unable to insert in production log");
                    }
                transaction.Commit();
       }
    }

可以帮忙。

谢谢,

4

1 回答 1

0

You are calling

                    session.SaveOrUpdate(l);

but this does not force the session to instantly update the database. NHibernate minimises calls to the database by storing all pending updates, and then calling them on the database in one batch. The SaveOrUpdate invocation is instructing the session to update the database with changes to l on the next Flush or Commit.

The oracle command is expecting the data to have already been written to the database.

I believe you can solve this problem with an explict call to Flush.

            using (ITransaction transaction = session.BeginTransaction())
            {

                try
                {
                    // Business Entity Saved in Tag1/Tag2 Table
                    session.SaveOrUpdate(l);
                    session.Flush();  // <<== Write all of our changes so far
                }
                catch (Exception ex)
                {
                    ErrorLogExceptionHandler.ErrorLog(ref ex);
                    throw new Exception("Unable to save data");
                }
于 2012-07-27T00:26:35.993 回答