Cannot create namespace in multi-document transaction(MongoDB 4.0, Spring Data 2.1.0, Spring Boot)

ⅰ亾dé卋堺 提交于 2021-01-20 07:20:52

问题


This question regarding to Spring boot, Mongo4.0, Spring-data application.

I have upgraded from MongoDB 3.6.x to MongoDB 4.0 and Spring data 2.0.x to Spring-data 2.1.0 for transactional management while inserting data into multiple documents. But here I face this issue while creating a database, collection and inserting a document into the collection.

Issue

Cannot create namespace sampledb_200.demo in multi-document transaction 

Here sampldb_200 is database name and demo is the collection name.

In earlier mongodb3.6.X versions, Spring Data 2.0.x, I was able to do database, collection creation, document insertion simultaneously even database and collection do not exist.

Using Spring Data 2.1.0, MongoDB 4.0, Database, collection creation, and insertion does not happen under one function and database, collection creation to be done explicitly.


回答1:


Spring is embracing a new feature shipped with MongoDB 4.0 that supports multi-document transactions. That feature works only for existing collections, ie, a multi-document transaction cannot include an insert operation that would result in the creation of a new collection. You should create your collections before hand to use this feature.




回答2:


So, if you are using mongoose transaction startTransaction and after you save some new document, with .save({sessions}) please make sure that you have created the correct collection for this document.

Cause, in that case, Mongo will not create the new collection automatically. Check the session option https://mongoosejs.com/docs/api.html#model_Model-save




回答3:


use this annotation on the top of your modal class

@org.springframework.data.mongodb.core.mapping.Document("DEMO")


来源:https://stackoverflow.com/questions/52585715/cannot-create-namespace-in-multi-document-transactionmongodb-4-0-spring-data-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!