spring-projects / spring-data-mongodb

Provides support to increase developer productivity in Java when using MongoDB. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
https://spring.io/projects/spring-data-mongodb/
Apache License 2.0
1.62k stars 1.09k forks source link

WriteResultChecking behavior #4034

Open sundarvenkata-EBI opened 2 years ago

sundarvenkata-EBI commented 2 years ago

I am a bit confused about the MongoTemplate WriteResultChecking variable: https://github.com/spring-projects/spring-data-mongodb/blob/1b7678a6af8c21e2584143ae215b18e50bcc6b7f/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java#L180 does this actively suppress write errors thrown by the MongoDB driver? This is what the document says: https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.reactive.template.writeresultchecking

We have a large Spring Data project where we have used MongoTemplates all over the place with a majority write concern. But we haven't set this variable. I am wondering if we should now be suspicious of every write that we had ever done because Spring might have thrown away any write exceptions from the driver due to this variable not being set and the failures went silent.

Also, it looks like I am not the only one to be concerned about this. There are also these 2 questions on SO which express a similar concern:

mrclrchtr commented 1 year ago

I am also confused... could someone who knows what the effects are please explain something about this?

garretwilson commented 1 year ago

I'm confused as well. I'm just now reading the latest documentation and stumbled upon this.

Does this mean that if I just create a basic Spring Boot MongoDB application, following the instructions in any Spring book (e.g. Spring in Action, Sixth Edition), then Spring Data MongoDB will be silently discarding any errors? Really? Please explain.

celiovasconcelos commented 1 year ago

I'm confused as well. I have the defaults but still get write exceptions like:

org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 112 (WriteConflict): 'WriteConflict error: this operation conflicted with another operation. Please retry your operation or multi-document transaction...

christophstrobl commented 1 year ago

Thanks for the heads up on this one, seems to have slipped through for quite a while. The WriteResultChecking dates back to the early days of the project when writes had often gone UNACKNOWLEDGED only reporting network issues not necessarily server errors. By setting the value to EXCEPTION the template encorces ACKNOWLEDGED writes if not WriteConvern has been specified. Otherwise the default WriteConvern of the template will be used. We'll revisit the documentation section to provide more insight.