-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-configuration for Spring Data MongoDB ignores spring.data.mongodb.database when spring.data.mongodb.uri has been set #35566
Comments
Thanks for the report. I don't think we can make the suggested change as the intention is that the connection details contain all of the information that's required to connect to Mongo. Can you please take a step back and describe the problem that you're facing? A sample that works with 3.0.x but does not work with 3.1.0 would be ideal. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as resolved.
This comment was marked as resolved.
Thanks for checking. yes it is failing starting from 3.0.x . As you have already confirmed the issue, I am just mentioned the logic straight away to the code from spring boot 2.6.6. As you can see the method properties.getMongoClientDatabase() returns the database name from "spring.data.mogodb.uri". if it is not available in uri it falling back to database property "spring.data.mongodb.database". Hence, I am expecting the same behaviour in 3.x.x as well.
|
Sorry, I am confused now as the issue title says "after upgrading to 3.1.x". Can you please clarify? |
This comment was marked as outdated.
This comment was marked as outdated.
I tried reproducing the issue by with spring boot 3.0.7. I don't see this error. I have attached the demo code.here. when I change the spring boot version to 3.1.1-SNAPSHOT I see the failure. spring boot 3.1.1-SNAPSHOT failing here. |
Good evening, I am facing the same error after update spring-boot-starter-parent from 3.0.1 to 3.1.0.
The link to he repository is https://github.com/LauroSilveira/alura-flix-api. Kind regards, |
If it adds context, My DB SaaS provider doesn't offer a connection string containing the DB name, it's for the instance instead. Overriding PropertiesMongoConnectionDetails seemed to be the easiest place to do that for me. @Bean
PropertiesMongoConnectionDetails mongoProperties(final MongoProperties mongoProperties) {
final String withDatabaseName =
mongoProperties.getUri().replace("/?", "/" + mongoProperties.getDatabase() + "?");
mongoProperties.setUri(withDatabaseName);
return new PropertiesMongoConnectionDetails(mongoProperties);
} |
@carldini This should work in your case. but, I think we should not change the value of a configurationProperty bean programmatically. Maybe, you could create a new object of MongoProperties and pass the new obj instead. |
Thanks, all. I now understand the problem. The
Prior to 3.1, the implementation did not align with that which is why it used to work. The behavior is now aligned with the documentation so In the meantime, it may be easiest to define your own @Bean
MongoDatabaseFactorySupport<?> mongoDatabaseFactory(MongoClient mongoClient, MongoProperties properties) {
return new SimpleMongoClientDatabaseFactory(mongoClient, properties.getDatabase());
} |
I think I'm changing my mind about this. The connection details are already sufficient to connect. It's only Spring Data Mongo that insists on a database which it ultimately uses to make a call to |
Hi, If it can help anyone who also face this issue. On a project after an update from 3.0.x to 3.1.x, I resolved the issue by a using properties overriding : spring.data.mongodb.database=demo-db
spring.data.mongodb.uri=mongodb://localhost:27018/?readPreference=primaryPreferred&directConnection=true with this : spring.data.mongodb.database=demo-db
spring.data.mongodb.uri=mongodb://localhost:27018/${spring.data.mongodb.database}?readPreference=primaryPreferred&directConnection=true In case you need authentication in the URI, you also need to add authentication database in the URI : spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=demo-db
spring.data.mongodb.uri=mongodb://username:password@localhost:27018/${spring.data.mongodb.database}?authSource=${spring.data.mongodb.authentication-database}&readPreference=primaryPreferred&directConnection=true My projects uses Yaml files instead of properties files but I expect the behavior to be the same regardless. Best regards |
Just a quick question: what I am experiencing on 3.1.0 is that the property |
@ctonsing It's hard to say without knowing more. Please try 3.1.1-SNAPSHOT (available from https://repo.spring.io/snapshot) and see if your problem is fixed. If it isn't, please open a new issue with a minimal sample that reproduces the problem. |
Thank you @wilkinsona, I did as suggested and the problem is fixed in current 3.1.1-SNAPSHOT. Thank you to all the devs for all your hard work! |
Thanks for trying the snapshot, @ctonsing. Much appreciated. |
After upgrading to spring-boot 3 mongo auto configuration is not working, when connection string is used.
I think we need to have a fallback option if
this.connectionDetails.getConnectionString().getDatabase()
is null.Can we change the above code as mentioned below?
The text was updated successfully, but these errors were encountered: