【问题标题】:Mongock migration failure with transaction带有事务的 Mongock 迁移失败
【发布时间】:2021-07-08 13:48:45
【问题描述】:

我正在运行一个 spring boot 2.4.3 应用程序,并且我有一个支持事务的 mongodb 4 集群(我使用 run-rs 在本地运行它)。我也在使用spring数据mongodb。我正在尝试为数据库迁移集成 mongock(最新版本 - 4.3.8),但我遇到了一个我无法解决的问题。正如我们所知,最新版本的 mongock 默认使用事务。我有以下变更集,它只是为我的一个实体创建了一个 mongodb 集合。

@ChangeSet(order = "001", id = "initSessionCollection", author = "Hristo")
public void init(MongockTemplate mongockTemplate) {
  mongockTemplate.createCollection(Session.class);
}

当我运行应用程序时,迁移失败并出现以下错误:

2021-04-13 16:36:18.092  WARN 70141 --- [           main] c.g.c.m.d.m.s.v.SpringDataMongoV3Driver  : Error in Mongock's transaction

com.github.cloudyrock.mongock.exception.MongockException: Error in method[InitDb.init] : Command failed with error 251 (NoSuchTransaction): 'Given transaction number 1 does not match any in-progress transactions.' on server localhost:27017. The full response is {"errorLabels": ["TransientTransactionError"], "operationTime": {"$timestamp": {"t": 1618320977, "i": 3}}, "ok": 0.0, "errmsg": "Given transaction number 1 does not match any in-progress transactions.", "code": 251, "codeName": "NoSuchTransaction", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1618320977, "i": 3}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}; nested exception is com.mongodb.MongoCommandException: Command failed with error 251 (NoSuchTransaction): 'Given transaction number 1 does not match any in-progress transactions.' on server localhost:27017. The full response is {"errorLabels": ["TransientTransactionError"], "operationTime": {"$timestamp": {"t": 1618320977, "i": 3}}, "ok": 0.0, "errmsg": "Given transaction number 1 does not match any in-progress transactions.", "code": 251, "codeName": "NoSuchTransaction", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1618320977, "i": 3}}, "signature": {"hash": {"$binary": {"base64": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "subType": "00"}}, "keyId": 0}}}

如果我按预期禁用 mongock 事务,则迁移成功应用。我不想为数据库迁移禁用事务,但我不知道问题出在哪里。

编辑:我发现了问题所在——因为 Spring boot 包含多文档事务,并且此类事务不支持创建集合命令。但是那我应该如何处理集合的创建呢?

【问题讨论】:

    标签: mongock


    【解决方案1】:

    正如您所提到的,MongoDB 事务模型有一些限制。请看官方MongoDB documentation

    考虑到这一点,您无法将该操作包装在事务中,因此如果您正在使用事务,则无法在标准 ChangeLog 中使用它。

    然而,好消息是,Mongock 团队一如既往地考虑了这一点,并将提供一种用于预交易操作的机制。

    我们预计会在 6 月左右准备好发布候选版本。但是,如果这是紧急需要,请给我们发送电子邮件至dev@cloudyrock.io,我们会尽力寻找解决方案。

    【讨论】:

    • 好的,谢谢。我的问题是您可以预期的所有插入操作都在事务中,这就是为什么在插入新文档时无法自动创建集合的原因。但我找到了一种通过启用索引自动创建来创建集合的方法,正如预期的那样创建集合。除此之外,我并不急于开发您开发的功能,但它是一个不错的功能,我会等待它。
    猜你喜欢
    • 1970-01-01
    • 2022-06-22
    • 2021-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 2020-07-28
    • 2018-11-10
    相关资源
    最近更新 更多