Repository navigation
Closed
Description
What you are doing?
return sequelize.transaction((tx) => {
return Topping.destroy({where: {pizza_id: request.params.id}, transaction: tx})
.then((res) => {
return Pizza.destroy({
where: {id: request.params.id},
transaction: tx
})
})
}).then((res) => reply('Pizza removed'))
.catch((err) => reply(err)
})What do you expect to happen?
A Transaction!
What is actually happening?
Unhandled rejection TypeError: Cannot read property 'query' of undefined
at parameters.length.Promise (/Users/me/projects/pizza-api/node_modules/sequelize/lib/dialects/postgres/query.js:65:53)
at Promise._execute (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/promise.js:483:18)
at new Promise (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/promise.js:79:10)
at Query.run (/Users/me/projects/pizza-api/node_modules/sequelize/lib/dialects/postgres/query.js:65:9)
at retry (/Users/me/projects/pizza-api/node_modules/sequelize/lib/sequelize.js:529:32)
at /Users/me/projects/pizza-api/node_modules/retry-as-promised/index.js:40:21
at Promise._execute (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/debuggability.js:300:9)
at Promise._resolveFromExecutor (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/promise.js:483:18)
at new Promise (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/promise.js:79:10)
at retryAsPromised (/Users/me/projects/pizza-api/node_modules/retry-as-promised/index.js:30:10)
at Promise.try.then.connection (/Users/me/projects/pizza-api/node_modules/sequelize/lib/sequelize.js:529:14)
at bound (domain.js:280:14)
at runBound (domain.js:293:12)
at tryCatcher (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromiseCtx (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/promise.js:606:10)
at Async._drainQueue (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/async.js:138:12)
at Async._drainQueues (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/Users/me/projects/pizza-api/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:666:20)
Dialect: postgres
Database version: 9.4.5.0
Sequelize version: 4.4.2
Bluebird version: 3.5.0
To note I downgraded to version Sequelize version: 3.29.0, code above goes through fine as expected as a transaction
Reacted by Phill Ayers, Travis Stodter, Alfredo El Ters, Axetroy, Prokop Simek, Vratislav Kalenda, Johannes Edelstam, Matt Huggins, Kyriakos Chatzidimitriou, Adam Savitzky and 21 more
Metadata
Metadata
Assignees
Labels
No labels
Activity
axetroy commentedon Aug 15, 2017
same problem.
prokopsimek commentedon Aug 15, 2017
same problem
mhuggins commentedon Aug 20, 2017
I'm also encountering this on sequelize 4.6.0 with pg 6.4.1.
mhuggins commentedon Aug 20, 2017
Downgrading to sequelize 3.29.0 fixed the problem for me as well.
ddura commentedon Aug 27, 2017
What version of pg module are you using? I am getting the same error but downgrading to 3.29.0 did not fix the problem for me.
mhuggins commentedon Aug 27, 2017
I'm using pg 6.4.1 with sequelize 3.30.4.
ryanSN commentedon Sep 5, 2017
@ddura pg 6.4.1
santiagodoldan commentedon Sep 25, 2017
We're also having this issue in production
pg 6.4.0 and sequelize 4.10.0
knowtheory commentedon Sep 29, 2017
Hey i bumped into this issue while using sequelize in an AWS Lambda function.
The issue ended up being the fact that i was initially instantiating a
sequelizeobject & database connections in adb.jsfile i was requiring at the top of my lambda function, rather than actually initializing the database connections w/in the lambda function itself.Why does that matter? Well if you're declaring stuff outside of your lambda function, AWS may keep your code hot, and reuse objects you've already declared (and just rerun your lambda function).
So, my lambda function was running fine on first run, and then happily closed down the db connection. All subsequent attempts to connect to the DB would toss the error that folks are seeing above.
I ended up wrapping all of my database code in a class which i could use to instantiate and then close down connections to my DB, and only instantiated that class from w/in the body of the lambda function.
Not positive that this is everybody's issue, but maybe this'll help some of you.
dskvr commentedon Oct 3, 2017
🤦♂️
santiagodoldan commentedon Oct 3, 2017
I was able to stop getting this issue increasing the pool of connections and setting a higher value to the new
acquireconfig.nutheory commentedon Oct 13, 2017
santiagodoldan solution worked perfectly for me... i was running transactional seeds that choked after a certain amount....
shanmarkus commentedon Nov 18, 2017
Any update on this ?
i'm using sequelize 4.13.5 and pg 6.2.0
ahlwong commentedon Nov 19, 2017
Just upgraded sequelize 4.22.8 and ran into this issue
Adding the default pool option in sequelize config helped me:
const pool = {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
};
gabegorelick commentedon Feb 8, 2018
Still seeing this on Sequelize v4.32.7 and pg v7.4.1. Set acquire to
20000and still see it.7 remaining items
papandreou commentedon Feb 14, 2018
@sushantdhiman, in the experiment I'm running, the
connection.connectcallback gets called twice, but only once per connection instance, both times with an artificialETIMEDOUTerror. In other words, it doesn't seem like thepgmodule does anything weird here.The function starting at
sequelize/lib/sequelize.js
Line 455 in 196a381
sequelize/lib/sequelize.js
Line 552 in 196a381
is executed,
options.transactionis there, butoptions.transaction.connectionis undefined. That explains why the promise is fulfilled withundefined.Are you able to reproduce it by patching the
pgmodule and trying to run a query in a transaction?holm commentedon Feb 14, 2018
I added a PR at #9051 which I believe fixes the issue
sushantdhiman commentedon Feb 16, 2018
This testcase is able to reproduce this issue with same stack as most have reported with. I think application is running out of connections as defined by pool configuration thus unable to acquire new connection because all other open connections are busy right now.
@holm fix doesn't seems to work for my testcase, does it work for your testcase @papandreou ?
I think we can throw a special error like
SequelizeConnectionTimeoutor something which tells you pool is full or connection was not acquired, rather than a cryptic error like this. For MySQL aleast it fails withResourceRequestTimeoutpapandreou commentedon Feb 16, 2018
@sushantdhiman, yes, @holm's fix does prevent the error in my test case.
holm commentedon Feb 16, 2018
Just to clarify. My PR will avoid the unhandled rejection, but the underlying reason for the connection not being established will not be fixed. However that rejection cause should still be returned directly from the main transaction promise.
I think it is likely that there is no one reason for that across all the problems reported here, but it does remove the noise of the unhandled rejection. I guess for the timeout case it could be an option to catch the error from the pool and rethrow some other error. I am not sure how much that actually helps though.
gabegorelick commentedon Feb 16, 2018
@sushantdhiman In this case, does Sequelize not retry the request? How should application code deal with full connection pools?
sushantdhiman commentedon Mar 20, 2018
🎉 This issue has been resolved in version 4.37.3 🎉
The release is available on:
Your semantic-release bot 📦🚀
sushantdhiman commentedon Mar 20, 2018
@gabegorelick Sorry for really late reply, but here goes
This is handled by
acquiretimeout, if application is fully utilizing pool and new connection is demanded thennode-pooldecide as maximum number of resources are being used let wait tillacquiretimeout. If any resource get released before firstResource Timeoutis triggered that resource is allocated to next waiting client.As you can see when pool is already full there is no need for creating new connection, pool is already at its limits. Only thing pool can do is to wait for any resource to get released.
In your application you can deal with this situation by setting
acquireabove your average transaction runtimegabegorelick commentedon Mar 22, 2018
Still seeing this error for other cases. I filed #9218 to track.
PaulMest commentedon Mar 6, 2019
My team was getting this error, but it was due to our own misuse -- we accidentally nested a transaction inside an object.
Here's a sample:
Note that the transaction is inside an unnecessary object. It should look like this:
marcogrcr commentedon Aug 26, 2020
For people experiencing issues in AWS Lambda, I have created a pull request that documents how to properly configure
sequelizefor Lambda. Hopefully you'll be able to see it in https://sequelize.org/master/manual/aws-lambda.html once it gets merged. In the meantime, please check #12642 out and provide feedback if you find any inaccuracies or space for improvement.mor-ndiaye commentedon Nov 29, 2020
Work well for me. Thanks man.
vatsalpatel68 commentedon May 8, 2021
mauro-n commentedon Jun 20, 2024
after being stuck on this for 1 hour i found that i forgot to add await before sequelize.transaction() and was passing a promise bogos