-
Notifications
You must be signed in to change notification settings - Fork 468
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
connection closed on query but connect seems to work #985
Comments
yes, the library does resolve even when not connected / closed
but no error, async / await successfully resolves this which is not correct imo This is the result of |
If you could actually provide the code and the errors and relevant configuration instead of poorly cropped screenshots, that would be infinitely more helpful in allowing me to assist you. At the moment I have no idea what you are doing or why it's not working, though I can already see one fairly obvious problem... |
Which would be? =) We've found a workaround by retrying the request after the first connect - it seems the server takes some time to open the connection. Perosnally I would have expected that connection.connected resolved always to true or throws an exception when it can not connect (hoist the connection error to
It's in the first post, this is everything that we use (just the connection details). The code snippet that already fails on our side: class Database {
async connect(){
try {
const connection = await db.connect({
user: `${process.env.DB_USER}`,
password: `${process.env.DB_PASS}`,
server: `${process.env.DB_HOST}`,
database: `${process.env.DB_DATABASE}`,
options: {
enableArithAbort: true
},
});
return connection;
} catch (err) {
throw new Error(error);
}
}
// @route /login
async login(username, password) {
// @todo: implement
}
// @route /authenticated
async authenticated(accessToken) {
try {
const connection = await this.connect();
if (!connection.connected) {
return { error: '-1' }
}
const result = await db.query...
...
The |
The obvious mistake I can see is here: } catch (err) {
throw new Error(error);
}
As for your code:
|
Thanks but this is not the problem =) It does not even throw there =)
We use the async / await variant of So nothing parallel here. |
You need to change your code to keep track of the fact the database is in a connecting state: class Database {
constructor() {
this.connecting = false;
this.connected = false;
this.connection = Promise.reject('Call connect() first');
}
async connect() {
if (!this.connected) {
if (!this.connecting) {
this.connecting = true;
this.connection = db.connect({
user: `${process.env.DB_USER}`,
password: `${process.env.DB_PASS}`,
server: `${process.env.DB_HOST}`,
database: `${process.env.DB_DATABASE}`,
options: {
enableArithAbort: true
},
}).then(connection => {
this.connected = true;
this.connecting = false;
return connection;
});
}
}
return this.connection;
}
async query(qry) {
await this.connect();
const result = await db.query(qry);
return result;
}
} This enforces the tl;dr of the comment I linked to (tl;dr only call sql.connect() once in your application.) |
Many thanks, this does indeed work. =) But retrying after this seems to work.
I would expect that it waits until it is connected and using async / await in all places should wait until a method resolves or throws. I think this can be improved in this library (at least I would expect that the promise / then based version can be simply used with the async / await version) =) Or this is not clearly documented. |
I'm not sure that |
It seems the async / await version has some issue with the
connect()
method and runningquery()
afterwards:It is not clear why this happsn after some time when we start the application.
Expected behaviour:
query()
should not fail asconnect()
was called before.Actual behaviour:
query()
fails due to closed connection to the SQl server.Configuration:
Software versions
The text was updated successfully, but these errors were encountered: