在Dart中连接到MySQL后需要3ms的延迟才能成功查询,为什么?为什么?

y4ekin9u  于 2023-10-13  发布在  Mysql
关注(0)|答案(1)|浏览(100)

您好Stack Overflow社区!
我在Dart中使用MySQL时遇到了一个特殊的问题。当我试图在建立连接后立即查询数据库时,我得到一个空结果。但是,如果我在查询之前添加3 ms的延迟,一切都会按预期工作。
下面是我的代码片段:

import 'package:mysql1/mysql1.dart';
import 'dart:async';

Future<void> main() async {
  final settings = ConnectionSettings(
    host: '127.0.0.1',
    port: 3306,
    user: 'root',
    password: 'root',
    db: 'base_mysql',
  );

  try {
    final MySqlConnection connection = await MySqlConnection.connect(settings);
    
    // Introducing a short delay before querying
    await Future.delayed(const Duration(milliseconds: 3));
    
    final results = await connection.query('SHOW TABLES');
    for (var row in results) {
      final tableName = row.values?.first;
      if (tableName != null) {
        print('Table Name: $tableName');
      }
    }

    await connection.close();

  } catch (e) {
    print('Error: $e');
  }
}

如果没有这个3 ms的延迟,第一个查询将返回一个空结果。此过程中的MySQL日志显示:

2023-10-02T10:24:48.514849Z    43 Connect   root@[client-ip] on base_mysql using TCP/IP
2023-10-02T10:24:48.519878Z    43 Query SHOW TABLES
2023-10-02T10:24:48.539763Z    43 Quit

我很好奇为什么会这样。有人遇到过类似的问题吗?我很感激任何关于这种行为的见解或解释。

nom7f22z

nom7f22z1#

我们使用以下解决方案。
如果发生错误,它可以重置连接并创建一个新的连接。
这些网站已经运行了几个月没有问题。

import 'dart:async';

import 'package:mysql1/mysql1.dart';

import 'yaml_config.dart';

Completer<MySqlConnection>? _connectionCompleter;

Future<MySqlConnection> getConnection() async {
  try {
    if (_connectionCompleter == null) {
      return _createConnection();
    } else {
      final connection = await _connectionCompleter!.future;
      await connection.query('SELECT 1');
      return connection;
    }
  } catch (e) {
    const knownMessages = [
      'Cannot write to socket, it is closed',
      'Socket has been closed',
    ];
    final message = '$e';
    for (final knownMessage in knownMessages) {
      if (message.contains(knownMessage)) {
        return _createConnection();
      }
    }

    rethrow;
  }
}

Future<MySqlConnection> _connect() async {
  final config = yamlConfig('config.yaml');
  final settings = ConnectionSettings(
      host: config.read('database.host'),
      port: config.read('database.port'),
      user: config.read('database.user'),
      password: config.read('database.password'),
      db: config.read('database.database'));
  final result = await MySqlConnection.connect(settings);
  return result;
}

Future<MySqlConnection> _createConnection() async {
  final completer = Completer<MySqlConnection>();
  _connectionCompleter = completer;
  try {
    final connection = await _connect();
    completer.complete(connection);
  } catch (e, s) {
    completer.completeError(e, s);
    _connectionCompleter = null;
    rethrow;
  }

  return completer.future;
}

相关问题