mongo-dart / mongo_dart

Mongo_dart: MongoDB driver for Dart programming language
https://pub.dev/packages/mongo_dart
MIT License
446 stars 98 forks source link

Question about connection #366

Closed Oskar888 closed 5 months ago

Oskar888 commented 7 months ago

Hello, I have a problem with how to connect to the database in my backend. Many clients will have access to the API, so I want good database connection support. Is it a good idea to simply use db.open() and db.close() after each single query? and how can I use the connection pool? I was also thinking about creating a singleton that will connect to the database on the first query? It would be useful to have some sample code on how to implement it :) Thank you in advance. PS. I'm writing here because I couldn't find another way to communicate with devs

giorgiofran commented 6 months ago

I would not suggest to open and close for any request. The idea is to let the connection open until the driver is closed. Then, as the server configuration may change, it is better to check for any call if the driver is connected or not. In that case it is better to close and reopen it again.

giorgiofran commented 6 months ago

For Q&A see here

giorgiofran commented 6 months ago

Give a look also to #368 . There is a good example of how to manage the connection