Closed Djyp closed 6 years ago
The code creates a new Connection() object and the use the DocumentManager to store documents.
What code creates the connection? Is this bundle's code?
FWIW the default mongodb://localhost:27017
is also used by underlying ext/mongo
so Doctrine's Connection is only mimicking lower level defaults.
It's our project that uses the Connection class. And I'm wondering if it's wrong to connect to MongoDb like this.
It's your bug then. You should explicitly specify where you want to connect (so new Connection(getenv(MONGODB_URL))
as per your comment). The Connection
can't know anything about env variables on its own. Now to answer your questions:
Did the developer used the proper way to connect to MongoDB through this Bundle ?
I'm not sure why did the developer did new Connection
on his own, but due to this I'm inclined to say "No". One should specify connection options through bundle's configuration and then get connections from DIC, not create them manually.
Don't you think it's a concern that the Connection class uses localhost by default ? (I know, it's on another repo).
No, as I said previously we're mimicking what the lower level driver does.
Thanks a lot for your answers ! :+1: Really helpful !
Hello everyone,
I was charged with putting a project in production. The project is using this bundle (so it's using doctrine/mongodb-odm and doctrine/mongodb as well). The project is under Symfony 4.1 and doesn't use much bundles. Just this one, OldSoundRabbitMqBundle and GuzzleHttp.
The problem
I had a hard time to make it work properly. Every launch of our command was throwing a "Connection refused" error, saying it was unable to reach
mongodb://localhost:27017
... of course ! I've configured mydoctrine.mongodb.yaml
with a connection on a different server ! There were no way localhost should have been called, ever. It took me a while to understand what happened...What happened
The code creates a
new Connection()
object and the use the DocumentManager to store documents. I discovered that the Connection class at line 267 was forcing the connection to localhost:27017 if nothing was specified to the constructor. I managed to solve the problem by creating the object like this :new Connection(getenv(MONGODB_URL))
.While the developer was using MongoDB on his machine, he never figured there was any problem.
I browsed documentations, github repos, blogs,... you name it, it was Googled and read twice or more... I checked the configurations, made sur the .env vars were loaded, the YAML config files were read and correctly formed. I figured out the situation by testing the whole process line by line, through our code and the bundles'.
My questions are...
Did the developer used the proper way to connect to MongoDB through this Bundle ? Don't you think it's a concern that the Connection class uses localhost by default ? (I know, it's on another repo).
Thanks for you answers