【问题标题】:Transaction allow to fail and continue事务允许失败并继续
【发布时间】:2014-12-31 19:15:36
【问题描述】:

我遇到了可由用户配置的主要数据迁移问题,可以说是这样的:

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: 当前事务被中止,命令被忽略直到 交易区块结束

有没有办法让我的交易允许失败的执行语句而不中止整个交易?

谢谢。

【问题讨论】:

  • Postgres 是否支持写入约束违规的异常表?这可能会抑制将事务标记为仅回滚的异常。
  • @Nicholas 你能解释一下吗?

标签: java sql postgresql transactions rollback


【解决方案1】:

尝试回滚事务并在 IndexDuplicityException 上启动一个新事务:

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

【讨论】:

  • 如果我这样做,我会丢失之前执行的所有语句。
  • @leomcpugo 那么每次迭代都设置一个保存点,如果发生异常你回滚到它呢?
  • 我想应该这样做!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多