detach method

Future<void> detach()

Closes the database connection.

After calling detach, the connection object cannot be used any longer. Closing the connection may take some time, as it requires closing all active queries and ending the background worker isolate. Also, in some cases an exception may be thrown, so detach should be called in a try ... catch context, like any other method which may fail.

Example:

final db = await FbDb.attach(host: "localhost", database: "employee");
// work with the database here using the db attachment object
await db.detach();
// db cannot be used any more

Implementation

Future<void> detach() async {
  try {
    await _closeActiveQueries();
    final resp = await _askWorker(FbDbControlOp.detach, []);
    if ((mem is TracingAllocator) &&
        resp.data.isNotEmpty &&
        resp.data[0] is Map) {
      _updateMemStats(resp.data[0]);
    }
  } catch (_) {
    _terminateWorker();
  } finally {
    _workerTerminated();
    _finalizer.detach(this);
  }
}