Skip to content
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

MongoDB ConnectionException SocketException #377

Open
DennisKragekjaer opened this issue Jun 9, 2024 · 4 comments
Open

MongoDB ConnectionException SocketException #377

DennisKragekjaer opened this issue Jun 9, 2024 · 4 comments

Comments

@DennisKragekjaer
Copy link

Hi, in my Flutter project I keep getting this:

MongoDB ConnectionException: connection closed: SocketException: Connection reset by peer (OS Error: Connection reset by peer, errno = 54)

Almost like every 5 min. Then when trying to call again, it works.
Its happening on iOS, both in simulator and on device. Does anyone know why that is popping up.

@giorgiofran
Copy link
Contributor

No idea. To which kind of server are you connecting to? Did you get a look on the internet about similar problems in flutter?

@DennisKragekjaer
Copy link
Author

Hi again.

I’m using mondodb Atlas.
Yea I searched the net for similar issues, but I didn’t find a solution to the problem.

I had to code some retry mechanism but it seems a bit weird to me that I need to do that.
I tried setting a timeout to calls.

@giorgiofran
Copy link
Contributor

Give a look to issue #328 .

@TimeLord2010
Copy link

I also noticed this. I created a script that migrated data from mysql to mongodb, Mysql was on the same machine, but mongo was on atlas and it is a serverless instance.

I would say that every 5 to 10 min I got this error and I needed to restart the script. I had to handle it by creating a function:

Future<T> persistentMongoOperation<T>(Future<T> Function() func) async {
  for (var i = 0; i < 2; i++) {
    try {
      var r = await func();
      return r;
    } on ConnectionException {
      logger.error('Connection exception: Retrying...');
      await Future.delayed(Duration(seconds: 2));
      await closeMongoConnection();
    }
  }
  throw Exception('Failed to process operation');
}

Where func is a method that is smart enough to cache the mongo connection and open the connection if not already.

Anyway, the thing is, the nodejs driver for mongodb has a retry option under retryWrites and retryReads fields.

Its not clear if these options are able to be passed in the connection string. That, I did not test. But if not, that would be a great thing to add in the dart package here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants