stablekernel / postgresql-dart

Dart PostgreSQL driver: supports extended query format, binary protocol and statement reuse.
https://www.dartdocs.org/documentation/postgres/latest
BSD 3-Clause "New" or "Revised" License
129 stars 32 forks source link

Attempting to execute query, but connection is not open #133

Closed wfe8006 closed 4 years ago

wfe8006 commented 4 years ago

So I create a master postgres connection that handles each http request. But for some reason I get connection is not open error from time to time and I have to restart my dart script manually. Any idea why?

PostgreSQLSeverity.error : Attempting to execute query, but connection is not open.
#0      _PostgreSQLExecutionContextMixin._query (package:postgres/src/connection.dart:423:7)
#1      _PostgreSQLExecutionContextMixin.query (package:postgres/src/connection.dart:404:7)
#2      _PostgreSQLExecutionContextMixin.mappedResultsQuery (package:postgres/src/connection.dart:454:22)
#3      User.getUsersHandler.<anonymous closure>.<anonymous closure> (package:test/user.dart:404:53)
#4      _RootZone.runUnary (dart:async/zone.dart:1381:54)
#5      _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
#6      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
#7      Future._propagateToListeners (dart:async/future_impl.dart:709:32)
#8      Future._completeWithValue (dart:async/future_impl.dart:524:5)
#9      Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:554:7)
#10     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
#11     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
#12     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#13     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:175:5)
isoos commented 4 years ago

Connections may get closed by various reasons:

It is generally best to use a connection pool that opens connections when needed, reuses them (with optional testing), and after a configurable connection use they close them and open a new one. If you are using package:aqueduct, you get something like that as part of the framework, or you could use package:postgres_pool to have a configurable pool.

wfe8006 commented 4 years ago

I had never faced this issue when I was with LEPP stack till I migrated the backend to Dart. (Dart and DB sit on the same server) I will check out the tips, thanks.

isoos commented 4 years ago

There isn't much Dart-specific here, I would assume that your PHP stack re-connected to the server on each request, or it may have had its own connection pooling.