【问题标题】:Sometime, there is a delay required to execute queries successful in Cockroachdb有时,在 Cockroachdb 中成功执行查询需要延迟
【发布时间】:2023-03-19 05:57:02
【问题描述】:

我正在尝试将我们的 PostgreSQL 数据库迁移到 Cockroach DB。因此,在我的本地(Macbook Pro)上设置了单节点服务器后,我正在运行我们的 moch 测试。有一些问题我一一解决,但最后一个。问题是测试(已通过)开始失败并显示以下错误消息:

Error: current transaction is aborted, commands ignored until end of transaction block
.

这是我的运行节点配置(在不安全模式下运行):

CockroachDB node starting at 2019-01-27 13:11:46.17270052 +0000 UTC (took 0.9s)
build:               CCL v2.1.1 @ 2018/11/19 18:20:16 (go1.10.3)
webui:               http://localhost:8080
sql:                 postgresql://root@localhost:26257?sslmode=disable
client flags:        cockroach <client cmd> --host=localhost:26257 --insecure

我尝试在插入记录后延迟 1 秒运行 sql 查询,它开始工作。

如果有人能解释为什么需要这种延迟以及任何解决方案,我将不胜感激。

【问题讨论】:

    标签: cockroachdb


    【解决方案1】:

    感谢罗恩。问题出在我尝试处理错误代码 40001(可重试错误)的代码中。处理这种情况的正确方法是在重试之前回滚事务,我没有这样做。正确的代码应该是这样的:

    .catch( (e) => {
      // check whether the error is "retryable error" (40001) or not. if yes,
      // retry the whole batch, if not rollback and then re-throw the exception
      return client.query('rollback').then(() => {
        if (e.code === '40001') {
          return batch(list)
        } else {
          throw e
        }
      })
    })
    

    但我做错了:

    // *** THIS IS THE WRONG CODE.
    .catch( (e) => {
      // check whether the error is "retryable error" (40001) or not. if yes,
      // retry the whole batch, if not rollback and then re-throw the exception
      if (e.code == 40001) {
        return batch(list)
      } else {
        return client.query('rollback').then(() => {
          throw e
        })
      }
    })
    

    所以导致事务中止的连接在放回连接池之前没有回滚,导致下次使用时出错。

    想想为什么1秒的延迟有助于克服这种情况,我猜想连接池管理器在空闲一秒后就终止了连接。

    【讨论】:

      【解决方案2】:

      您看到的错误是由于尝试在事务上下文中发出另一个命令已被中止。

      但是如果没有看到导致它中止的事务内部发生了什么,我将无法判断事务中止的原因。

      能否分享您正在使用的 DML、DDL 和架构?

      谢谢,

      罗恩

      【讨论】:

        猜你喜欢
        • 2012-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-07
        • 1970-01-01
        相关资源
        最近更新 更多