我正在运行一个spring boot 2.4.3应用程序,并且我有一个支持事务的mongodb 4集群(我使用run -rs在本地运行它)。我也在使用spring data 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包含多文档事务,而在此类事务中不支持create collection命令。但是,我应该如何处理集合的创建呢?
发布于 2021-04-15 20:29:16
正如您所提到的,MongoDB事务模型有一些限制。请参考官方MongoDB documentation。
考虑到这一点,您不能将该操作包装在事务中,因此如果您正在使用事务,则不能在标准ChangeLog中使用它。
然而,好的新消息是,我认为,一如既往:D,Mongock团队考虑到了这一点,并将为预交易操作提供一种机制。
我们希望在六月左右有一个发布的候选版本。但是,如果这是一个紧急需求,请发送电子邮件给我们的dev@cloudyrock.io,我们将尝试找到解决方案。
https://stackoverflow.com/questions/67076023
复制相似问题