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
TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call? #2820
Comments
From the documentation:
So if you're using sqlite3 you will run into issues with more than 1 connection. |
Oh got it, so the issue is: many clients are backed up in a queue waiting for a single available connection to sqlite3, but the timeout passes and that error is thrown? As a quick fix for a hobby project, I wonder if it's acceptable to simply increase the timeout |
I am facing this issue though i am just trying to create a table in sample project , only one client is connected still this error , Any fix for this ? This is related to #2824 |
+1 having this issue too |
@tahv0 there are 100 different reasons why that might happen, so you need to find out how to reproduce the problem. Probably by then you will also see why that is happening. |
Knex version: 0.15.2 I'm having the same connection issue when I run "knex migrate:latest", and have scoured google with no luck. I've tested connections between Node and MySQL with a simple SQL script and that's connecting fine. Initially there was an authentication issue with the connection which I was able to resolve by altering the user privileges with 'mysql_native_password' which has to be done in MySQL 8 to be able to support legacy connections I believe. However I was unable to get Knex the connection going. Does Knex support MySQL 8? The examples in the documentation show MySQL 5.6 and there is no explicit mention of MySQL 8 support. I know this can be anything, so here's all the code I have for this project. knexfile.js require('dotenv').config();
module.exports = {
development: {
client: 'mysql2',
connection: {
port: process.env.db_port,
host: process.env.db_host,
database: process.env.db_name,
user: process.env.db_user,
password: process.env.db_key,
connectTimeout: 90000
},
debug: true,
pool: {
min: 1,
max: 20,
},
migrations: {
directory: __dirname + '/knex/migrations',
},
seeds: {
directory: __dirname + '/knex/seeds',
},
}
}; knex.js require('dotenv').config();
const env = process.env.environment || 'development'
const config = require('../knexfile.js')[env];
const knex = require('knex')(config);
module.exports = knex; server.js require('dotenv').config();
const express = require('express');
const bodyParser = require("body-parser");
const path = require('path');
const PORT = process.env.db_port || 3002;
const knex = require('../knex/knex.js');
const app = express();
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(`${__dirname}/../client/dist`, { maxAge: "365d" }));
app.listen(PORT, () => {
console.log(`listening on port: ${PORT}`);
}); migration file exports.up = function(knex, Promise) {
return knex.schema.createTable('sdcapstone', (t) => {
t.increments('_id').primary().unique();
t.string('name').notNullable();
t.integer('rating');
t.integer('reviewCount');
t.integer('itemNum').notNullable();
t.integer('price').notNullable();
t.string('mainImage');
t.json('images');
})
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('sdcapstone');
}; Here is the result of a console log of "knex" from knex.js
|
Update, I've reverted back to MySQL version 5.7.24 and I'm still getting the same timeout error |
For me this was Google Cloud specific problem, because one can not take connection from GAE to Google SQL with public IP. I fixed problem by reading some more docs. |
@tahv0 I am having the same issue with Google Cloud. Can you share the docs that helped you fix this problem? |
@ashanker2 |
@tahv0 Thank you for the doc. Seems like we have incorporated these steps in our setup but will verify it again. Is there anything specific you had to do to fix this problem? |
@ashanker2 Does your code work with locally installed postgresql server? |
@ashanker2 and not ip&port. GAE mounts tcp/unix socket to a container when you have configured Cloud SQL instance. There may be a problem if db is in a different region than service that uses it and you may need to use this beta_settings-flag in your .yaml like in the docs. |
@tahv0 Thank you. It worked with INSTANCE_CONNECTION_NAME instead of Host & Port. |
I solved this problem with these versions:
|
@batadamnjanovic Thank you so much! I've been pulling my hair trying to understand what the hell happened. Upgrading to the latest |
for me it was a silly error, I ended up not putting the |
@joaotanaca Good way to avoid that error is to use implicit commit/rollback style of transactions
|
I didn't end up checking the transaction method, thank you very much |
i am getting this on a small not-yet-public site with no more than 1-2 connections at a time and im not using transactions, on postgres nothing about my setup is particularly complicated, and the db isn't very big |
@rossPatton try to isolate the exact case where it is happening and then you might find what is wrong in your app. Maybe your queries are really slow. Anyways that error is pretty hard to create without flooding huge amount of queries or using transactions. |
This issue popped up for me today on a a very simple and small project that has been working fine for weeks and whose code hasn't changed in weeks. I am not doing any transactions and there was never more than 2 connections, and those only had to handle less than 10 very small queries a day. The only thing that I could find to fix this was to upgrade to the latest version of Knex. As soon as I did that this issue went away, but I'm left feeling a little nervous as I don't understand what could have caused the issue for me in the first place. |
@jj0b What was the old version of knex you were using? For example one old bug which caused connections closed by the server, because of timeout to be not rejected from pool, could have caused that kind of behavior. To be sure for your use case is probably good to set pool min: 0 and connection idle timeout to be smaller than connection timeout of the server (few minutes). |
@elhigu I had been using knex 0.20.15. My Express server had been running fine for weeks, and this issue only appeared yesterday on my local development version, which was only being queried a few times a day by only two clients. My staging and production versions which are similarly very low traffic with only two connections max have not so far had this issue. I'll take a look at the settings you recommend. Thank you. :) |
Not really sure. All I know is that I hadn't made any changes recently and everything had been working when this error popped up yesterday when querying the database. All I did to fix it was update the latest version of Knex and the error stopped happening. I didn't update anything else, including Node as it seems some had to in that thread you shared. I also haven't been doing any migrations which seemed to have triggered this issue for some. |
This feeling when you think you'll spend the entire day trying to fix such a damn issue, but you find the right spot and the right people that ease the pain in a flash 😃 |
Hi! guys in my application I was facing (sometime in between) My config of knex is
and I'm using it something like
|
I got the same error but the reason was VPC |
Issue for me were the inbound rules as well, helps to do a quick |
Hi devs, i have the same problem with my application. Today it has a service that crashes the application and I need to restart. I've tried a lot of things but I wasn't successful. Here is my error and current configuration.
Version: "knex": "^0.21.1" |
Maybe this code works better, which implicitly closes transactions also in case of an error...
|
Hey there, I too had this error in development, and I wasn't explicitly using transactions. Maybe one of the objection.js codes I was using was, but I doubt it given logs. It turns out I was running docker-compose with postgres:13.2-alpine on my MacBook m1 computer. The alpine version is built for amd64 architectures, while m1 MacBooks are running on arm64 architectures. While this works with Docker, it's not recommended and can lead to errors like "stuck spinlock detected at LWLockWaitListLock" and "core dumped" in your Postgres logs (see docker/for-mac#5122) which is the root cause of Knex/Tarn not being able to get more connections (tl;dr; the database goes into crazy mode) So I switched back to just using the image postgres:13.2, and it works 👍, no more errors :) |
It sounds like this error is really ambiguous. Anyway to make it more clear? Do you expect it to be thrown from any knex ORM query like I'm finding that this error is consistently reproducable by running a function of mine that makes additional queries in parallel with lots of input. |
In all the error logs for this, I'm seeing there is no additional info on where the error was thrown from
There's no file or line number to trace back to your module. sql and bindings is undefined I think reproducing the error locally is acceptable, but before that, it would help to actually understand which calls or queries are throwing. I would recommend adding your own stack trace in your using node.js 14
|
for me increasing the aquireTimeout was the solution |
run into the same problem with the strapi and had to modify the following to get the connection reliably.
|
Can you format your reply better? |
here you go for Strapi version 3.5 module.exports = ({ env }) => ({ Strapi version 4.5.4 If you would any one would like to try self hosting one can try these steps https://medium.com/@mohanganesh/strapi-headless-cms-google-cloud-run-and-postgresql-6126b597b10c |
For anyone running into this issue, let me explain what learned here. I was getting this same error with my database connection because the The end result was that Knex would consider all instances of this error its own timeout error. Knex would also timeout itself if the I was able to completely fix the problem by converting all runtime secret loading to ENV variables that are filled during deploy (you can do this natively via most hosting providers ... in my case it was Cloud Run on GCP). The nightmare is finally over. |
I'm unfortunately getting this error still after reading this thread. I'm following @mohan-ganesh GCP article and haven't a clue on how to resolve this error. Any additional suggestions? [2023-04-26 15:41:06.732] debug: ⛔️ Server wasn't able to start properly.
[2023-04-26 15:41:06.737] error: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call? |
Following back up, Mohan was able to help me solve the issue on my end. I was confused on GCP cloud run ports where I tried setting the port to 1337 rather than 8080. Since cloud run only runs on 8080 it was throwing the error. On top of that, I had to add my variables into my cloud run env. Once completed, it worked like a charm. Again, thanks @mohan-ganesh for helping me debug. |
@evanrosa We have been running our web application on port 8080 with CloudRun for several years and have the same problem. Could you please tell me why changing the port to 1337 would stop this problem? |
@evanrosa Could you explain exactly what the fix is? I tried running the cloud application port on 8080 and 1337, all with no luck. I even tried changing the web app port (strapi) from 1337 to 8080, also with no luck. |
hey Evan, the actual changes are documented at #2820 (comment) |
I also forgot to close out a transaction resulting in a connection leak. I figured out that I can add an assertion to my tests that assures I've not leaked any connections by checking the following knex.client.pool.numUsed() === 0 I'm on knex 2.4.2 which uses tarn 3.0.2 (the pooling library that has the numUsed function) |
Environment
Knex version: 0.15.2
Database: sqlite3
OS: Ubuntu 18
I'm getting lots of errors like
TimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
Any ideas? All my code lives in this one file: https://github.com/umaar/wiki-globe/blob/master/index.js - that code powers this globe which I posted in a Hacker News comment and in turn led to the discovery of such error.
An example query looks like this:
Does sqlite3 only allow a single connection to it because it's file-based? I'll try adding
.transacting()
calls to see if that makes a difference.I guess it comes down to: some technical limitation with sqlite3 (maybe lack of pooling?) or some badly written code DB/queries on my part! Any insight you have would be much appreciated, thanks!
The text was updated successfully, but these errors were encountered: