Socket Closed
mado-bohsali opened this issue · 9 comments
Unhandled Exception: Bad state: Cannot write to socket, it is closed (UPDATE statement)
@adamlofts kindly advise.
@mado-bohsali @hutuguaner have you been able to resolve this? Any help is appreciated!
I don't recall dear @Fraa-124
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.
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.
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?
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;
}
}