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

add a way to automatically reconnect in case mysql/mariadb database is restarted #122

Open
insinfo opened this issue Feb 9, 2022 · 1 comment

Comments

@insinfo
Copy link

insinfo commented Feb 9, 2022

add a way to automatically reconnect if the mysql/mariadb database is restarted, I think this would be very easy to implement, just save the connection settings and check if it is necessary to reconnect every time you run a query.

Future<dynamic> reconnectIfNecessary() async {
  try {
    await this.query('select true');
    return this;
  } catch (e) {
//when the database restarts there is a loss of connection
    if ('$e'.contains('Cannot write to socket, it is closed')) {
      var settings = new ConnectionSettings(
          host: mysqlConnInfoSite.host,
          port: mysqlConnInfoSite.port,
          user: mysqlConnInfoSite.username,
          password: mysqlConnInfoSite.password,
          db: mysqlConnInfoSite.database);
       await this.connect(settings);
      return this;
    }
    rethrow;
  }
}
@wustrong
Copy link

good

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

2 participants