0
レコードを2つのデータベースに保存する トランザクションによって例外がスローされた場合 データベースTransaction
はreverted
になる 私のコードにどのように実装できますか?ここで 同じHibernate Transactionで2つのデータベースレコードを保存する方法は?
- まず私はセカンダリセッショントランザクション
secondaryTx.commit();
- をやってる私のサンプルコード私の上記のコードでは実際に
public void save(Vendor vendor, Ledger ledger) throws Exception { Transaction primaryTx = null, secondaryTx = null; Session primarySession = null, secondarySession = null; try { secondarySession = HibernateUtil.getSession("secondarydb"); secondaryTx = secondarySession.beginTransaction(); secondarySession.save(ledger); vendor.setLedgerId(ledger.getId()); primarySession = HibernateUtil.getSession("primarydb"); primaryTx = primarySession.beginTransaction(); primarySession.saveOrUpdate(vendor); secondaryTx.commit(); primaryTx.commit(); } catch (Exception e) { if (secondaryTx != null) { secondaryTx.rollback(); } if (primaryTx != null) { primaryTx.rollback(); } throw e; } finally { if (secondarySession != null && secondarySession.isOpen()) { secondarySession.close(); } if (primarySession != null && primarySession.isOpen()) { primarySession.close(); } } }
ある
primaryTx .commit();
- プライマリ取引で例外が発生した場合
rollBack
の
- しかし
Secondary Transaction
データReverted
取得し、Primary Transaction
が正常に私は両方のトランザクションデータを戻すことができますどのように - を差し戻していませんか?
ありがとうございます。うまくいきました。 –