【问题标题】:How to save two databases records in same Hibernate Transaction?如何在同一个 Hibernate Transaction 中保存两个数据库记录?
【发布时间】:2016-07-07 12:53:53
【问题描述】:

我想将记录保存在两个数据库中 如果事务抛出任何异常 两个数据库Transaction 都应该得到reverted 如何在我的代码中实现?

这是我的示例代码

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

其实在我上面的代码中

  1. 首先我正在做辅助会话事务secondaryTx.commit();
  2. 然后我在做主会话事务primaryTx .commit();
  3. 如果我在主事务rollBack 中遇到任何异常,应该完成
  4. 但是Secondary Transaction数据没有得到RevertedPrimary Transaction成功还原
  5. 如何恢复两个交易数据?

【问题讨论】:

    标签: mysql database hibernate session transactions


    【解决方案1】:
        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);
            secondarySession.flush(); // add this line
            primarySession.flush(); // add these line
            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();
            }
        }
    }
    
    1. flush before commit.它会抛出异常。所以你可以成功回滚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-11
      • 1970-01-01
      • 2018-02-23
      • 2013-05-07
      相关资源
      最近更新 更多