postgresql-dart icon indicating copy to clipboard operation
postgresql-dart copied to clipboard

when i run the delete from ccc where id=1 an throw a error.

Open normidar opened this issue 5 years ago • 9 comments

delete from ccc where id=1; when I run the delete from ccc where id=1 and throw an error. it is like this: ERROR: Bad state: StreamSink is closed

delete from ccc where id=1;
ERROR: Bad state: StreamSink is closed
dart:io                                                    _Socket.add
package:postgres/src/query.dart 95:12                      Query.sendExtended
package:postgres/src/connection_fsm.dart 197:9             _PostgreSQLConnectionStateIdle.processQuery
package:postgres/src/connection_fsm.dart 183:14            _PostgreSQLConnectionStateIdle.awake
package:postgres/src/connection_fsm.dart 214:12            _PostgreSQLConnectionStateIdle.onEnter
package:postgres/src/connection.dart 234:41                PostgreSQLConnection._transitionToState
package:postgres/src/connection.dart 263:11                PostgreSQLConnection._readData
===== asynchronous gap ===========================
dart:async                                                 _Completer.completeError
package:postgres/src/query.dart 169:17                     Query.completeError
package:postgres/src/connection_fsm.dart 202:11            _PostgreSQLConnectionStateIdle.processQuery.<fn>
===== asynchronous gap ===========================
dart:async                                                 scheduleMicrotask
package:postgres/src/connection_fsm.dart 201:7             _PostgreSQLConnectionStateIdle.processQuery
package:postgres/src/connection_fsm.dart 183:14            _PostgreSQLConnectionStateIdle.awake
package:postgres/src/connection_fsm.dart 214:12            _PostgreSQLConnectionStateIdle.onEnter
package:postgres/src/connection.dart 234:41                PostgreSQLConnection._transitionToState
package:postgres/src/connection.dart 263:11                PostgreSQLConnection._readData
===== asynchronous gap ===========================
dart:io                                                    _Socket.listen
package:postgres/src/connection.dart 149:15                PostgreSQLConnection.open
===== asynchronous gap ===========================
dart:async                                                 _asyncThenWrapperHelper
package:fig_database_linker/dataLinkerPostgres.dart 57:17  DataLinkerPostgres.getConn
test/postgres_test.dart 67:16                              main.<fn>

normidar avatar Aug 21 '20 16:08 normidar

the image

normidar avatar Aug 21 '20 16:08 normidar

Can you open a connection and run SELECT 1; on it?

isoos avatar Aug 21 '20 17:08 isoos

Can you open a connection and run SELECT 1; on it?

when I run SELECT 1;, it is no error.

And when I unclose the database link it is no error too. is it can not close after DELETE?

normidar avatar Aug 22 '20 08:08 normidar

At one point you are closing the connection. Could you please share how you are using calling it?

isoos avatar Aug 22 '20 08:08 isoos

At one point you are closing the connection. Could you please share how you are using calling it?

my code is:

.....
var sql = 'DELETE FROM $table WHERE "' + idName +'" = ' + id +';';
await _conn.query(sql)
if(!_conn.isClosed)await _conn.close();

the _conn is PostgreSQLConnection class.

I'm using Postgres in this website https://www.elephantsql.com/

normidar avatar Aug 22 '20 08:08 normidar

is it can not close after the drop and the delete? when I close after the drop it has an error too.

normidar avatar Aug 22 '20 08:08 normidar

Do you do anything else between opening the connection and querying it? Please provide full example (without the connection string).

isoos avatar Aug 22 '20 08:08 isoos

Do you do anything else between opening the connection and querying it? Please provide full example (without the connection string).

  LinkSets card = LinkSets(
    'raja.db.elephantsql.com',
    '5432',
    'wabjtpfp',
    'Q5q05nP9mcDUY9V3bWidtwegWxU9IO_J',
    'wabjtpfp',
  );
  Future<String> getConn() async{
    _conn = PostgreSQLConnection(
      card.host,
      int.parse(card.port ?? 5432),
      card.db,
      username: card.user,
      password: card.psw,
    );
    await _conn.open();
    return _conn.isClosed.toString();
  }
  Future deleteTable(String table)async{
    _getRows('DROP TABLE ' + table + ';');
  }
  Future<List<List<dynamic>>> _getRows(String sql)async{
    List<List<dynamic>> results = await _getRealRows(sql);
    return results;
  }
  Future<PostgreSQLResult> _getRealRows(String sql){
    return _conn.query(sql);
  }
  Future closeDatabase()async{
    if(!_conn.isClosed)await _conn.close();
  }

    await link.getConn();
    await link.deleteTable('ccc');
    await link.closeDatabase();

you can link it whit this setting, maybe it is wrong in the server, I do not know.

normidar avatar Aug 22 '20 08:08 normidar

  • You have shared the DB password, I'd suggest to change it for good.
  • You have a missing return in deleteTable, e.g. return _getRows('DROP TABLE ' + table + ';');

I'd suggest to add unawaited_futures to you analysis_options.yaml as an extra lint.

isoos avatar Aug 22 '20 08:08 isoos