2012-07-26 47 views
2

在窗口應用程序中,我們使用的是nHibernate。在更新數據表(Tag1或Tag2)時我們遇到問題,並且在同一個ISession中,我們使用Oracle Package將表中的數據插入另一個表(QA表)。在提交Oracle包沒有看到Tag1/Tag2表中已更改的數據,因此修改後的數據未在QA表中更新,可能是因爲在同一會話中調用了嗎?nHibernate不通過Oracle Package在同一會話中更新其他表

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(); 
     } 
    } 

有些幫助。

感謝,

+0

「l」從哪裏來?它是否來自您用來調用「SaveOrUpdate」的同一會話? – 2012-07-26 06:54:47

+0

「l」是我們用「session.SaveOrUpdate(l)」保存的業務實體;「然後在同一個會話中,我們將一個Oracle包「IDbCommand db = ProductionLog.ProductionLogUpdate(l.ProductionlogSeqNo,loadAction)作爲DbCommand」調用,將數據移動到QA表中。 – 2012-07-26 07:04:56

回答

0

要調用

    session.SaveOrUpdate(l); 

但這並不強迫進程立即更新數據庫。 NHibernate通過存儲所有掛起的更新,然後在一個批處理中在數據庫中調用它們來最小化對數據庫的調用。調用SaveOrUpdate指示會話更新數據庫,更改爲l,接下來的FlushCommit

oracle命令期望數據已寫入數據庫。

我相信你可以用明確的電話號碼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"); 
       } 
+0

謝謝,這工作 – 2012-08-11 04:18:56

相關問題