Using the pg
module and clients pool I need to call done()
method in order to return the client into clients pool.
Once I connect to the server, I add SQL query client’s query queue and I start handling the result asynchronously row by row in row event:
// Execute SQL query
var query = client.query("SELECT * FROM categories");
// Handle every row asynchronously
query.on('row', handleRow );
When I should call done()
method?
Should I call it once I receive the end
event and all rows are processed or I can call it immediately after I add SQL query to the client’s query queue?
Going from an example on this project's page (https://github.com/brianc/node-pg-query-stream), I'd recommend calling it when you get the
end
event.This makes sense, because you're not
done
with it until you're received the last row. If someone else got that same connection and tried using it, that would likely create odd errors.