2014-11-04 37 views
0

林與可由用戶配置的主要數據遷移,可以說,它是這樣的:交易允許失敗,並繼續有問題

try { 
    daoManager.beginTransaction(); 

    for (Entity node : nodeList) { 

     boolean inserted; 

     if (migrationConfig.canInsert()) { 
      try { 
       inserted = daoManager.getDaoEntity().insert(node); 
      } 
      catch(IndexDuplicityException ex) { 
       inserted = false; 
       //I want the transaction to continues when this Exceptions is 
       //throwed 
      } 
     } 

     if (migrationConfig.canUpdate() && !inserted) { 
      modificoEntidad = daoManager.getDaoEntity().update(node); 
     } 
    } 

    //Later 

    if (//Business Logic) { 
     throw new MustRollBackTransaction(); 
    } 

    daoManager.commitTransaction(); 

} catch (MustRollBackTransaction ex) { 
    daoManager.RollBack(); 
} 

我的問題是,當IndexDuplicityException被倒掉,任何未來的交易將被忽略,給我這個例外

PSQLException:忽略當前事務被中止,命令,直到事務塊 結束

沒有辦法讓我的交易允許失敗的excecuted語句,而不是中止整個交易?

謝謝。

+0

不支持Postgres的異常表?這可能會抑制將事務標記爲僅回滾的異常。 – Nicholas 2014-11-05 11:38:39

+0

@尼古拉斯你能解釋一下嗎? – leomcpugo 2014-11-06 15:42:08

回答

0

嘗試回滾事務,並開始IndexDuplicityException一個新問題:其中違反約束被寫入

}catch(IndexDuplicityException ex) { 
    inserted = false;  
    daoManager.RollBack(); 
    daoManager.beginTransaction(); 
    } 
+0

如果我這樣做,我會失去所有以前執行的語句。 – leomcpugo 2014-11-05 21:32:59

+0

@leomcpugo那麼每次迭代設置一個保存點以及如果發生異常,就會回滾到它呢? – rafalopez79 2014-11-05 21:39:40

+0

我想應該這樣做!謝謝! – leomcpugo 2014-11-06 15:40:25