close method

  1. @override
Future<void> close()
override

Releases resources owned by this database.

Implementations must allow this method to be called more than once.

Implementation

@override
Future<void> close() {
  if (_insideTransaction) {
    return Future<void>.error(
      const SqlException(
        SqlErrorCode.unsupported,
        'The parent database cannot be closed inside its transaction.',
      ),
    );
  }
  final existing = _closeFuture;
  if (existing != null) return existing;

  _closing = true;
  final pool = _pool;
  return _closeFuture = pool == null
      ? _serial.run(() async {
          final connection = _connection!;
          if (!_singleConnectionUsable) {
            connection.getSocket().destroy();
            return;
          }
          if (!connection.connected) return;
          await runMysql(
            connection.close,
            operation: 'close',
            connected: true,
          );
        })
      : _closePool(pool);
}