You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auto-Reconnect or How to listen for disconnections ? #198
In my dart server I want to hold a few connections to a mongodb.
In case of disconnection of the mongodb server (such as network failure),
I would like to auto-reconnect. Currently it seems to simply throw the following error:
throw MongoDartError('No master connection');
How is it possible to auto-reconnect?
Or if it is not implemented It would be nice If i could listen for disconnections so I could write some code to re-connect.
If I missed it, I'm sorry.
Regards
Reacted by Mat Simon, Jonas Wanke, Bob Jackman, Mayur Dhurpate, David Wimmer, Max Korbel and Hán Trung Kiên
You are right.
I thought about this problem, but I couldn't find any good solution.
My doubt was which algorithm to implement. I mean, If it is a network problem, maybe retrying a couple of times could be enough, but, if it is a server problem we could end retrying indefinitely. So, even a fixed number of retries could not be a correct solution.
I ended up doing nothing.
Maybe your suggestion of a listener is a good solution. Anyone else has a suggestion?
Briefly, at present in my server I test if it is connected before of each operation (or small logically linked group of operations).
I do check the db.masterconnection.connected flag (it is a quite new getter). If it is false I close and reopen the database.
I know that it is not an elegant solution, but it works.
Hi, I am hosting a server (gRPC, not REST, but I think this is irrelevant) and I created the following scenario. I do a first request with the database already open and everything works fine. I am checking if the database is open and reopening if not. If I shutdown MongoDb and start it up again and then perform another request, this works flawlessly. Then I will shutdown the server, do a request and it will fail, as expected. Now, however, because I made a request while MongoDb was down, I ended doing a call to db.open while the db server is unavailable. Next, I start MongoDb once more and do another request. The call to db.open will fail because it says it is in an invalid state. I did try to do a call to db.close() while the state was opening, but it also threw. So, what I ended doing was the following code:
// I am not so sure if I really need the _db.state == State.OPENING conditionif (!_db.isConnected || _db.state ==State.OPENING) {
if (_db.state ==State.OPENING) {
_db.state =State.CLOSED; // I am manually updating the state to CLOSE to prevent the invalid state exception.
}
await _db.open();
}
It works, though it seems like a hack. I am running MongoDb as a replica-set.
You are right. There was an inconsistency in how the "OPENING" state was managed. The idea of "OPENING" is to prevent different calls to db.open() at the same time, so your hack cannot be a definitive solution
I made a fix and posted on GitHub.
You can try it temporarily changing your pubspec.yaml settings to the following for mongo_dart dependency:
It is much, much, better now. I am doing some stress testing and the client performs several requests in burst sequence. After starting MongoDb again, the very first request to db.open() is working fine now. The following few requests will throw, and then further requests will be OK. Though it's not completely desirable behavior, it is kind of expected and understandable. The first request asks to create the connection, and then the others will find a connection that is still not open, but is underway trying to be opened, so it throws with a complaint that the state is still opening. Maybe I'll just leave it as it is or later on add a flag and some code in lieu of a semaphore, which Dart does not have.
Anyhow, your fix solves the problem. Thanks you! 😊
Actually I have more than an idea, but a companion package to share in a future date. MongoDB drivers for C# actually map the bson they receive from MongoDb into plain-old C# objects, objects of classes that represent the business domain. So, in C#, I never need to work with the equivalent of Map<String, dynamic>, but only with stuff like Person, Contract, Car, Tree,..., and so on. I wanted to have the same thing with Dart, so I have been working on a prototype of a code generator that creates code that receives Map and does the conversion to business domain objects and back. It is inspired by json_serializable, but doesn't add it as a dependency (the problem I am solving is very similar). The problem with my package is that I am trying to solve too many problems on that prototype and now I need to split the implementation and do a whole lot of refactoring before I share it. That same prototype does a whole lot of other things. Because I am planning on using gRPC, my prototype is also generating the corresponding .proto files, which are then fed into protoc to generate the Dart representation of protocol buffer message. My code generator also does the mapping between my business domain classes and those generated protobuffer dart classes. Finally, for each Class, the code generator also generates a Validator class that adds validation methods for custom validation annotations I defined. An example:
@mapclassPerson
{
ObjectId id; // by default will be mapped to '_id' when the object is mapped to Map<String, dynamic>@requiredString name;
@MapField(name:'dob') // will be mapped to 'dob' key instead of birthDateDateTime birthDate;
List<Contract> contracts;
@mapIgnoreint dummy; // not mapped
}
Class person will have a class PersonMapper. Class Contract will also have a ContractMapper. PersonMapper will map all the fields into Map<String, dynamic> and back, using ContractMapper to map each contract in Contracts, and so on. There will also be a new generated person.proto file. gRPC related protoc utility also generates classes and I am prefixing them with G (configurable), so the equivalent protoc class will be GPerson. My code generator also creates a PersonProtoMapper that will map between Person and GPerson.
It will also create a PersonValidator that will tell if the instance is valid according to business rules. In this example there is only one rule, that name must be a none-empty string. Finally, in case Person is an immutable class, the code generator also creates a PersonBuilder, a corresponding mutable class that has a build() method to generate immutable Person instances. As if all this weren't enough, I am also generating classes to represent CRUD permission points for each of the business classes.
The purpose of all this mess and complexity is actually to simplify the development workflow. In the end, each business concept will be represented by a single root source of truth, from which everything else is generated code, thus avoiding tons of manually created boilerplate. As soon as I find the time to split this into more fine-grained packages, I intend sharing this code on github. If you like the idea and would like to integrate this into mongo-dart project, say, for instance as mongo_dart_generator, it would be fine by me.
Interesting. I do have a similar environment, but it is too complex and tailored on my needs to be shared. I'd like to make a simpler version, but at present I have no time...
What if we have a function db.onDisconnect({required Function function})?
It could trigger a function every time the db change it state to disconnect, then would be possible to reconnect and keep the connection on
The idea is to have a client (like NodeJs driver), and let the client manage the connections. This is in the work list, but it is a complex task, that implies also some breaking changes.
As I had to spend a lot of time with the null-safety version, I cannot start immediately.
Aside the automatic management from the client, my intention is to let the user be informed about connection status (if required), with a mechanism similar to the one you pointed out.
I cannot tell you when it will be ready... :-(
You are right. I thought about this problem, but I couldn't find any good solution. My doubt was which algorithm to implement. I mean, If it is a network problem, maybe retrying a couple of times could be enough, but, if it is a server problem we could end retrying indefinitely. So, even a fixed number of retries could not be a correct solution. I ended up doing nothing. Maybe your suggestion of a listener is a good solution. _Anyone else has a suggestion? ...
How about a backoff strategy where the retries get further and further apart? I've seen other clients (message queues, databases, etc) also implement a maxRetries configuration. An event stream could be helpful as a way to allow the user to handle and implmement their own reconnection logic. Personally, I had to fully wrap both Db and Collection classes like so:
Obviously, this won't work for everyone, (besides being a ton of work to override every single method), since it'll just indefinitely continue trying to reconnect, but maybe it can provide some inspiration. (it also implements a whenReady completer so your main script can do something like await db.whenReady)
Thanks for your suggestion. My idea is to take example from tne Nodejs driver and manage messaging on the status and auto-reconnect based on a polling check every 2-3 seconds.
I have started recently to give a look to this solution, but I have a few time in this period, so it will take a long time :-(
Any news on this?, I've come to realise my implementation isn't that great😓, well one of them at least but I still need a better one.
If it not much of a bother, a simple explanation on how to do it better or a short code snippet would be highly appreciated, I'd be glad to see how y'all approached it.
Give a look to discussion #228 . Maybe it could help.
Would it be a good idea to use those reconnect methods in a Middleware for a web server to verify connection before each api call and return an internal server error or something on failure to reconnect? Or is it better before each DB call like they use it?
[...] I test if it is connected before of each operation [...] db.masterconnection.connected . If it is false I close and reopen the database. I know that it is not an elegant solution, but it works.
But you'd be missing on connection pools, which are the industry standards ;
They make database operations 10-40x faster and prevent resource exhaustion.
Since an unreachable db makes mast server useless I suggest this approach on the server side :
import'dart:io';
import'package:mongo_pool/mongo_pool.dart';
/// Database middleware that manages MongoDB connections using a connection pool////// This middleware acquires a connection from the provided pool,/// passes it to the endpoint function, and ensures proper release even if errors occur.////// It also provides a [radical] answer to the bloody MongoDartError('No master connection')/// Which can put your service down for several minutes or more/// Assuming you are deploying on a modern [cloudProvider]/// If the error is encountered the middleware will simply [terminate] the server/// which the [cloudProvider] will automatically reboot/// A lightweight Dart container should restart in less than 5 seconds///////// Parameters:/// - [poolService]: The MongoDB connection pool service that provides and manages database connections/// - [endpoint]: The function that receives a database connection and performs operations/// - [maxRetries]: The maximum number of retries to attempt before giving up/// - [retryDelay]: The delay between retries///////// Example usage:/// ```dart/// final poolService = MongoDbPoolService(configuration);/// await poolService.initialize();////// final result = await databaseMiddleware(/// poolService,/// (db) async {/// final collection = db.collection('my_collection');/// return await collection.find().toList();/// }/// );/// ```Future<T> databaseMiddleware<T>(
MongoDbPoolService poolService,
Future<T> Function(Db) endpoint, {
int maxRetries =3,
Duration retryDelay =constDuration(seconds:2),
}) async {
for (int attempt =1; attempt <= maxRetries; attempt++) {
try {
final db =await poolService.acquire();
try {
final result =awaitendpoint(db);
return result;
} finally {
poolService.release(db);
}
} catch (e) {
if (e.toString().contains('No master connection')) {
print('Database connection error (attempt $attempt/$maxRetries): $e');
if (attempt < maxRetries) {
print('Retrying in ${retryDelay.inSeconds} seconds...');
awaitFuture.delayed(retryDelay);
continue;
} else {
// Last attempt failed - restart the serverprint(
'MongoDartError No master connection error persists after $maxRetries attempts. Kill the server. CloudProvider will restart it.');
await poolService.close(); // Clean up connectionsexit(1); // Let the process manager restart it
}
}
rethrow;
}
}
throwException('This should never be reached but it makes the compiler happy');
}
Activity
giorgiofran commentedon Sep 29, 2020
You are right.
I thought about this problem, but I couldn't find any good solution.
My doubt was which algorithm to implement. I mean, If it is a network problem, maybe retrying a couple of times could be enough, but, if it is a server problem we could end retrying indefinitely. So, even a fixed number of retries could not be a correct solution.
I ended up doing nothing.
Maybe your suggestion of a listener is a good solution.
Anyone else has a suggestion?
Briefly, at present in my server I test if it is connected before of each operation (or small logically linked group of operations).
I do check the
db.masterconnection.connectedflag (it is a quite new getter). If it is false I close and reopen the database.I know that it is not an elegant solution, but it works.
hiroshihorie commentedon Sep 30, 2020
Thanks for your reply.
For now, I will just use a simple timer to monitor
db.masterconnection.connectedand re-connect if disconnected.Regards
ruicraveiro commentedon Oct 29, 2020
Hi, I am hosting a server (gRPC, not REST, but I think this is irrelevant) and I created the following scenario. I do a first request with the database already open and everything works fine. I am checking if the database is open and reopening if not. If I shutdown MongoDb and start it up again and then perform another request, this works flawlessly. Then I will shutdown the server, do a request and it will fail, as expected. Now, however, because I made a request while MongoDb was down, I ended doing a call to db.open while the db server is unavailable. Next, I start MongoDb once more and do another request. The call to db.open will fail because it says it is in an invalid state. I did try to do a call to db.close() while the state was opening, but it also threw. So, what I ended doing was the following code:
It works, though it seems like a hack. I am running MongoDb as a replica-set.
Kind Regards,
Rui
giorgiofran commentedon Oct 30, 2020
You are right. There was an inconsistency in how the "
OPENING" state was managed. The idea of "OPENING" is to prevent different calls todb.open()at the same time, so your hack cannot be a definitive solutionI made a fix and posted on GitHub.
You can try it temporarily changing your
pubspec.yamlsettings to the following formongo_dartdependency:Could you please give a try and tell me if everything is OK? I would greatly appreciate.
Once tested I will publish it.
ruicraveiro commentedon Oct 30, 2020
It is much, much, better now. I am doing some stress testing and the client performs several requests in burst sequence. After starting MongoDb again, the very first request to db.open() is working fine now. The following few requests will throw, and then further requests will be OK. Though it's not completely desirable behavior, it is kind of expected and understandable. The first request asks to create the connection, and then the others will find a connection that is still not open, but is underway trying to be opened, so it throws with a complaint that the state is still opening. Maybe I'll just leave it as it is or later on add a flag and some code in lieu of a semaphore, which Dart does not have.
Anyhow, your fix solves the problem. Thanks you! 😊
Best Regards!
Rui
giorgiofran commentedon Oct 30, 2020
Published.
If you have any idea on how to improve the package, please let me know.
I'm quite busy right now, but I will surely consider it.
ruicraveiro commentedon Oct 30, 2020
Actually I have more than an idea, but a companion package to share in a future date. MongoDB drivers for C# actually map the bson they receive from MongoDb into plain-old C# objects, objects of classes that represent the business domain. So, in C#, I never need to work with the equivalent of Map<String, dynamic>, but only with stuff like Person, Contract, Car, Tree,..., and so on. I wanted to have the same thing with Dart, so I have been working on a prototype of a code generator that creates code that receives Map and does the conversion to business domain objects and back. It is inspired by json_serializable, but doesn't add it as a dependency (the problem I am solving is very similar). The problem with my package is that I am trying to solve too many problems on that prototype and now I need to split the implementation and do a whole lot of refactoring before I share it. That same prototype does a whole lot of other things. Because I am planning on using gRPC, my prototype is also generating the corresponding .proto files, which are then fed into protoc to generate the Dart representation of protocol buffer message. My code generator also does the mapping between my business domain classes and those generated protobuffer dart classes. Finally, for each Class, the code generator also generates a Validator class that adds validation methods for custom validation annotations I defined. An example:
Class person will have a class PersonMapper. Class Contract will also have a ContractMapper. PersonMapper will map all the fields into Map<String, dynamic> and back, using ContractMapper to map each contract in Contracts, and so on. There will also be a new generated person.proto file. gRPC related protoc utility also generates classes and I am prefixing them with G (configurable), so the equivalent protoc class will be GPerson. My code generator also creates a PersonProtoMapper that will map between Person and GPerson.
It will also create a PersonValidator that will tell if the instance is valid according to business rules. In this example there is only one rule, that name must be a none-empty string. Finally, in case Person is an immutable class, the code generator also creates a PersonBuilder, a corresponding mutable class that has a build() method to generate immutable Person instances. As if all this weren't enough, I am also generating classes to represent CRUD permission points for each of the business classes.
The purpose of all this mess and complexity is actually to simplify the development workflow. In the end, each business concept will be represented by a single root source of truth, from which everything else is generated code, thus avoiding tons of manually created boilerplate. As soon as I find the time to split this into more fine-grained packages, I intend sharing this code on github. If you like the idea and would like to integrate this into mongo-dart project, say, for instance as mongo_dart_generator, it would be fine by me.
giorgiofran commentedon Oct 30, 2020
Interesting. I do have a similar environment, but it is too complex and tailored on my needs to be shared. I'd like to make a simpler version, but at present I have no time...
brunoggt commentedon Apr 8, 2021
What if we have a function db.onDisconnect({required Function function})?
It could trigger a function every time the db change it state to disconnect, then would be possible to reconnect and keep the connection on
giorgiofran commentedon Apr 8, 2021
The idea is to have a client (like NodeJs driver), and let the client manage the connections. This is in the work list, but it is a complex task, that implies also some breaking changes.
As I had to spend a lot of time with the null-safety version, I cannot start immediately.
Aside the automatic management from the client, my intention is to let the user be informed about connection status (if required), with a mechanism similar to the one you pointed out.
I cannot tell you when it will be ready... :-(
bobjackman commentedon Mar 8, 2022
Are there any updates on this reconnection effort? Currently running into it myself.
giorgiofran commentedon Mar 9, 2022
Unfortunately no news
bobjackman commentedon Mar 9, 2022
A couple years ago you posted the following:
How about a backoff strategy where the retries get further and further apart? I've seen other clients (message queues, databases, etc) also implement a maxRetries configuration. An event stream could be helpful as a way to allow the user to handle and implmement their own reconnection logic. Personally, I had to fully wrap both
DbandCollectionclasses like so:Obviously, this won't work for everyone, (besides being a ton of work to override every single method), since it'll just indefinitely continue trying to reconnect, but maybe it can provide some inspiration. (it also implements a
whenReadycompleter so your main script can do something likeawait db.whenReady)giorgiofran commentedon Mar 12, 2022
Thanks for your suggestion. My idea is to take example from tne Nodejs driver and manage messaging on the status and auto-reconnect based on a polling check every 2-3 seconds.
I have started recently to give a look to this solution, but I have a few time in this period, so it will take a long time :-(
JohnF17 commentedon Mar 29, 2023
Any news on this?, I've come to realise my implementation isn't that great😓, well one of them at least but I still need a better one.
If it not much of a bother, a simple explanation on how to do it better or a short code snippet would be highly appreciated, I'd be glad to see how y'all approached it.
giorgiofran commentedon Apr 13, 2023
Give a look to discussion #228 . Maybe it could help.
FillipMatthew commentedon May 17, 2023
Would it be a good idea to use those reconnect methods in a Middleware for a web server to verify connection before each api call and return an internal server error or something on failure to reconnect? Or is it better before each DB call like they use it?
quick test fix
flutter-painter commentedon Oct 24, 2025
As @giorgiofran said you could do this indeed :
But you'd be missing on connection pools, which are the industry standards ;
They make database operations 10-40x faster and prevent resource exhaustion.
Since an unreachable db makes mast server useless I suggest this approach on the server side :