我正在运行一个 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 包含多文档事务,并且此类事务不支持创建集合命令。但是,我应该如何处理集合的创建?