mysql1_dart icon indicating copy to clipboard operation
mysql1_dart copied to clipboard

Socket Closed

Open mohamad-elbohsaly opened this issue 3 years ago • 9 comments

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

mohamad-elbohsaly avatar Aug 30 '21 20:08 mohamad-elbohsaly

@adamlofts kindly advise.

mohamad-elbohsaly avatar Sep 02 '21 18:09 mohamad-elbohsaly

i meet this question too 微信图片_20210906083453

hutuguaner avatar Sep 06 '21 00:09 hutuguaner

i meet this question too 微信图片_20210906083453

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

mohamad-elbohsaly avatar Sep 13 '21 16:09 mohamad-elbohsaly

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

Fraa-124 avatar Jan 17 '22 13:01 Fraa-124

I don't recall dear @Fraa-124

mohamad-elbohsaly avatar Jan 17 '22 19:01 mohamad-elbohsaly

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.

ValdirGiorgi avatar Apr 17 '23 14:04 ValdirGiorgi

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.

rodrigobrazz avatar Apr 17 '23 16:04 rodrigobrazz

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?

ValdirGiorgi avatar Apr 17 '23 18:04 ValdirGiorgi

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

rodrigobrazz avatar Apr 17 '23 18:04 rodrigobrazz