connect method
Initiate connection to database. To close connection, invoke MySQLConnection.close method.
Default timeoutMs
is 10000 milliseconds
Implementation
Future<void> connect({int timeoutMs = 10000}) async {
if (_state != _MySQLConnectionState.fresh) {
throw MySQLClientException("Can not connect: status is not fresh");
}
_timeoutMs = timeoutMs;
_state = _MySQLConnectionState.waitInitialHandshake;
_socketSubscription = _socket.listen((data) {
for (final chunk in _splitPackets(data)) {
_processSocketData(chunk)
.onError((error, stackTrace) => _lastError = error);
}
});
_socketSubscription!.onDone(() {
_handleSocketClose();
});
// wait for connection established
await Future.doWhile(() async {
if (_lastError != null) {
final err = _lastError;
_forceClose();
throw err!;
}
if (_state == _MySQLConnectionState.connectionEstablished) {
return false;
}
await Future.delayed(Duration(milliseconds: 100));
return true;
}).timeout(Duration(
milliseconds: timeoutMs,
));
// set connection charset
await execute(
'SET @@collation_connection=$_collation, @@character_set_client=utf8mb4, @@character_set_connection=utf8mb4, @@character_set_results=utf8mb4',
);
}