You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dart连接MySQL后需3ms延迟才可成功查询,原因何在?

Dart连接MySQL后立即查询返回空,添加延迟才正常的原因?

我在使用Dart操作MySQL时遇到一个奇怪的问题——建立连接后立即执行查询会返回空结果,但添加3ms延迟后查询就能正常工作。以下是我的代码片段:

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

不添加这3ms延迟时,首次查询返回空结果。该过程的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  

我很好奇这一现象的原因,请问有人遇到过类似问题吗?希望能得到相关见解或解释。


内容的提问来源于stack exchange,提问作者googlewalker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 20:07:43