mysql1_dart icon indicating copy to clipboard operation
mysql1_dart copied to clipboard

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

Open insinfo opened this issue 3 years ago • 1 comments

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;
  }
}

insinfo avatar Feb 09 '22 17:02 insinfo

good

wustrong avatar Feb 17 '22 14:02 wustrong