adamlofts / mysql1_dart

MySQL driver for Dart
Other
134 stars 45 forks source link

Socket Closed #99

Open mado-bohsali opened 3 years ago

mado-bohsali commented 3 years ago

Unhandled Exception: Bad state: Cannot write to socket, it is closed (UPDATE statement)

mado-bohsali commented 3 years ago

@adamlofts kindly advise.

hutuguaner commented 3 years ago

i meet this question too

微信图片_20210906083453
mado-bohsali commented 3 years ago

i meet this question too

微信图片_20210906083453

You meant to say "you are are solving it"?

Fraa-124 commented 2 years ago

@mado-bohsali @hutuguaner have you been able to resolve this? Any help is appreciated!

mado-bohsali commented 2 years ago

I don't recall dear @Fraa-124

ValdirGiorgi commented 1 year ago

Has anyone managed to solve this problem, it seems to be mysql that closes the idle connection and in the morning when I go to use the application this error always appears and I have to restart it,

Contextualizing: I have an application running on the backend and it opens this connection with mysql when started and when it takes a while to get no calls to the database it generates this idleness and when we use it in the morning it shows the error.

rodrigobrazz commented 1 year ago

I had the same problem and I solved it by checking the open connection time... if 3 minutes passed, I close the connection and open it again using the same connection method. Another possibility is to handle this exception... whenever this type, close the connection and open it again.

ValdirGiorgi commented 1 year ago

I had the same problem and I solved it by checking the open connection time... if 3 minutes passed, I close the connection and open it again using the same connection method. Another possibility is to handle this exception... whenever this type, close the connection and open it again.

how did you control the time? Do you have a code example to show me?

rodrigobrazz commented 1 year ago

Singleton!

class MysqlAppConnection { static MysqlAppConnection? _instance;

MysqlAppConnection._();

static MysqlAppConnection get i { instance ??= MysqlAppConnection.(); return _instance!; }

// conexão com o banco ---------------------- final _mysqlHost = ''; final _mysqlPort = ; final _mysqlUser = ''; final _mysqlPass = ''; final _mysqlDb = ''; final _mysqlTimeOut = 20; // ------------------------------------------

bool _isOpen = false;

bool get isOpen => _isOpen; set isOpen(oppened) => _isOpen = oppened;

DateTime? _connectionTime; DateTime get connectionTime => _connectionTime ?? DateTime.now(); set connectionTime(time) => _connectionTime = time;

MySqlConnection? _connection;

Future<MySqlConnection?> connection() async { Duration duration = DateTime.now().difference(connectionTime);

if (duration.inMinutes >= 3) {
  await close();
}

try {
  if (!_isOpen) {
    _connection = await MySqlConnection.connect(
      ConnectionSettings(
        host: _mysqlHost,
        port: _mysqlPort,
        user: _mysqlUser,
        password: _mysqlPass,
        db: _mysqlDb,
        timeout: Duration(seconds: _mysqlTimeOut),
      ),
    );

    if (_connection == null) {
      throw DaoException(
          message: 'Conenction error (timeout)');
    }

    isOpen = true;
    connectionTime = DateTime.now();
    log('CONNECTION OPENNED');
  } else {
    log('ALREADY CONNECTED');
  }
} on MySqlException catch (e, s) {
  log('Error', error: e, stackTrace: s);
  throw DaoException(
      message: 'Connection error (${e.toString()})');
} on SocketException catch (e, s) {
  log('Socket error', error: e, stackTrace: s);
  throw DaoException(
      message: 'Socket error (${e.toString()})');
}
return _connection;

}

Future close() async { log('CLOSE CONNECTION'); try { if (isOpen || _connection != null) { await _connection?.close(); } } catch (e, s) { log('Error', error: e, stackTrace: s); } connectionTime = null; isOpen = false; } }