File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -247,6 +247,40 @@ cannot be re-connected by design.
247
247
This logic will also be part of connection pool support once I add that to this
248
248
library.
249
249
250
+ ## Idle connections
251
+
252
+ Connections emit ` 'drain' ` events after they have finished all queued queries
253
+ (including query callbacks). This event could be used to detect idle
254
+ connections:
255
+
256
+ ``` javascript
257
+ function detectIdle (connection ) {
258
+ connection ._idleTimeout = null ;
259
+
260
+ function clearIdleTimeout () {
261
+ if (connection .idleTimeout ) {
262
+ clearTimeout (connection .idleTimeout );
263
+ }
264
+ }
265
+
266
+ connection .on (' drain' , function () {
267
+ clearIdleTimeout ();
268
+ connection ._idleTimeout = setTimeout (60000 , function () {
269
+ console .error (" Connection was idle for 60 seconds" );
270
+ // or return to a pool, etc.
271
+ })
272
+ })
273
+
274
+ var query = connection .query ;
275
+ connection .query = function () {
276
+ clearIdleTimeout ();
277
+ query .apply (connection, arguments );
278
+ };
279
+ })
280
+
281
+ handleDisconnect (connection);
282
+ ```
283
+
250
284
## Escaping query values
251
285
252
286
In order to avoid SQL Injection attacks, you should always escape any user
You can’t perform that action at this time.
0 commit comments