-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Client re-connection doesn't work #1352
Comments
I’m almost certain clients aren’t meant to be connected more than once (@brianc, could you confirm this?); treat them as unusable after calling - var client = new pg.Client(connection);
-
function test() {
+ var client = new pg.Client(connection);
+
client.connect(err => {
if (err) {
console.log(err);
return;
}
client.query('select 123', [], (err, data) => {
if (err) {
console.log(err);
} else {
console.log('DATA:', data.rows[0]);
}
client.end();
});
});
} |
And what is the reason for that? |
They’re wrappers around a connection and should be associated with one connection. |
Yeah though it's not really documented clients are cheap to instantiate and should be considered 'used up' once they've been disconnected. There's a bit of a state machine inside the client w/ a bunch of event handlers being established after the connect event. Your best bet is going to be to throw the old one away & make a new one. The connection handshake over tcp is the part that takes a little bit of time, but reusing an existing client wouldn't save any time there as reconnecting would still need to happen - it would also introduce additional complexity in ensuring the old event handlers were disposed and the new ones set up correctly. I'll re-open this & add it to the 7.0 milestone to return an error if a client has |
It would be nice! The |
Clients are not reusable. This changes the client to raise errors whenever you try to reconnect a client that's already been used. They're cheap to create: just instantiate a new one (or use the pool) 😉. Closes #1352
Clients are not reusable. This changes the client to raise errors whenever you try to reconnect a client that's already been used. They're cheap to create: just instantiate a new one (or use the pool) 😉. Closes #1352
Closed with #1365 |
I have a similar problem. `const jwtLogin = new JwtStrategy(jwtOptions, function(payload, done) { console.log('after connect'); client.connect();
}); The first time I hit my endpoint that runs this code, everything works fine. I get my data back from the database fetch and it console.logged "client has been disconnected" but when I hit this endpoint again, my code only console.log('after connect') and none of my other code runs. I get Am I using client.end() incorrectly? |
@waruwaruwaru Don’t end the client if you want to continue using it. If you want to create a new client each time instead of keeping one open, you’ll have to do just that ( |
@charmander Thank you. |
Someone might get misunderstood the comment above, therefore I shall note:
|
I'm running a simple test against the latest
pg
, under Node.js 8.1.2:And it outputs the following, every time:
After I successfully call
client.end()
, it will successfully re-connect, successfully execute the query, just won't return any data. This looks odd.And this -
error: invalid frontend message type 0
doesn't make any sense.Could somebody, please explain, why we get such an odd output?
UPDATE
And if just after creating the client, I add the following:
The output gets absolutely flooded with a huge error dumb, so big, I can't even paste it here.
The text was updated successfully, but these errors were encountered: