Home > Enterprise >  Dart code fails to terminate execution when calling mysql
Dart code fails to terminate execution when calling mysql

Time:11-21

I am trying to use dart to access a MySQL db, my code executes completely but it doesn't terminate, I have to click the stop button on vscode to end it. I'm using mysql_client, but the same thing happened with mysql1.

import 'package:mysql_client/mysql_client.dart';

Future<void> main() async {
  final pool = MySQLConnectionPool(
      host: '127.0.0.1',
      port: 13306,
      userName: 'root',
      password: 'password',
      databaseName: 'dart_mysql',
      maxConnections: 10);

  var result = await pool.execute('select * from aluno');
  
  for (final row in result.rows) {
    print(row.assoc());
  }

  print('end of program');
}

Output shows this:

{id: 2, nome: asd}
{id: 11, nome: ASDAS}
{id: 12, nome: ASDAS}
{id: 13, nome: ASDAS}
{id: 14, nome: ASDAS}
end of program

I don't have any problems with other async code.

CodePudding user response:

When dealing with SQL connections it is normal that we need to tell when we are done using the SQL connection. By using a pool, we can be a bit more intelligent with the connections by reusing them for multiple different queries.

But we still need to tell when we are done using our resources. MySQLConnectionPool does therefore have a close() method we should call when we are done using the pool.

  •  Tags:  
  • dart
  • Related