schultek / stormberry

Access your postgres database effortlessly from dart code.
https://pub.dev/packages/stormberry
MIT License
68 stars 17 forks source link

PostgreSQLException (PostgreSQLSeverity.error : Query cancelled due to the database connection closing.) #58

Closed petrovyuri closed 1 year ago

petrovyuri commented 1 year ago

Hi, thank you very much for your wonderful plugin. I'm facing a very strange problem. After Postgres returns an error about the uniqueness of names, then the error about the lack of connection to the database is constantly returned.

Снимок экрана 2023-03-24 в 17 24 17

[log] PostgreSQLException (PostgreSQLSeverity.error 23505: duplicate key value violates unique constraint "__unique" Detail: Key (email, username)=(1, 1) already exists. Table: user_models Constraint __unique ) [log] #0 _PostgreSQLExecutionContextMixin._query connection.dart:514

1 _PostgreSQLExecutionContextMixin.query

connection.dart:475

2 Database.query

database.dart:91

#3 _UserModelRepository.insert user_model.schema.dart:41 #4 Database.runTransaction database.dart:163 #5 AuthMethodsService.signUp auth_methods_service.dart:56 #6 new Stream.fromFuture. (dart:async/stream.dart:247:17) [log] Error SignUp [log] PostgreSQLException (PostgreSQLSeverity.error : Query cancelled due to the database connection closing. ) [log] Error SignUp [log] PostgreSQLException (PostgreSQLSeverity.error : Query cancelled due to the database connection closing. ) [log] Error SignUp [log] PostgreSQLException (PostgreSQLSeverity.error : Query cancelled due to the database connection closing. ) [log] Error SignUp
petrovyuri commented 1 year ago

Solved problem, check connection and reinit. if (db.connection().isClosed) { db = initDatabase(); }

vfiruz97 commented 10 months ago

@petrovyuri Hey! I have the same problem in the production. And I think when errors come from plugin then we get closed connection. As my db variable is created in the main method and is passed to the repository models then after this I get constantly issue with closed connection.

I am going to try your approach before creating an issue about it.

jifferon commented 2 months ago

Solved problem, check connection and reinit. if (db.connection().isClosed) { db = initDatabase(); }

This is extremely bad code, because your initDatabase MUST be in a completely separate layer of abstractions than your method which actually performs any request to check for it's isClosed state...