mysql1_dart
mysql1_dart copied to clipboard
add a way to automatically reconnect in case mysql/mariadb database is restarted
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;
}
}
good