Reputation: 495
I have a nodejs project using loopback and postgressql.
I got a function handleTrade
like this, simply start the transaction with timeout 30s, query the Trade by id and do some updates:
async function handleTrade(id) {
const { Trade } = app.models;
const transaction = await Trade.beginTransaction({
timeout: 30000 // 30 seconds
});
try {
console.log(`${id} - 1`);
const trade = await Trade.findById(id, { transaction });
console.log(`${id} - 2`);
if (trade) {
// Do some updates with the trade
}
transaction.commit();
} catch (e) {
console.log(`${id} - 3`, e);
transaction.rollback();
}
}
I have a list of 10 ids, I want to handle all of them asynchronously and print Done
when everything is done.
async function test(ids) {
const promises = ids.map(id => handleTrade(id));
await Promise.all(promises);
console.log('Done');
process.exit(0);
}
test([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
And here's the log:
2 - Step 1
3 - Step 1
1 - Step 1
4 - Step 1
6 - Step 1
5 - Step 1
10 - Step 1
9 - Step 1
8 - Step 1
7 - Step 1
-- The process stuck here for 30 seconds (which is the timeout value of the transaction)
2 - Step 2
Unhandled rejection Error: The transaction is not active: 914ca760-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
9 - Step 2
1 - Step 2
3 - Step 2
6 - Step 2
4 - Step 2
5 - Step 2
10 - Step 2
8 - Step 2
Unhandled rejection Error: The transaction is not active: 914e5510-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cce71-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cce70-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cf581-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cf580-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cf582-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914cf583-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
Unhandled rejection Error: The transaction is not active: 914e5511-b804-11e8-bb6c-094b32725638
at process.nextTick (/Users/admin/Work/NIX/nix-project/engine/node_modules/loopback-datasource-juggler/lib/transaction.js:202:12)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
7 - Step 2
Done
The processes got stuck for 30 seconds after step 1, then when transaction timeout, it return the Error The transaction is not active
And this only happen when I have 10 ids, if I only use 9, it run smoothly
test([1, 2, 3, 4, 5, 6, 7, 8, 9]);
// Logs
2 - Step 1
4 - Step 1
5 - Step 1
9 - Step 1
7 - Step 1
8 - Step 1
6 - Step 1
1 - Step 1
3 - Step 1
2 - Step 2
4 - Step 2
9 - Step 2
8 - Step 2
1 - Step 2
5 - Step 2
7 - Step 2
6 - Step 2
3 - Step 2
Done
If you have any idea please help me. Thanks
Upvotes: 1
Views: 1407
Reputation: 46
try edit your datasource file.
example: my dataresources.json
old:
{
"db": {
"url": "${DATABASE_URL}",
"name": "db",
"connector": "postgresql"
}
}
new:
{
"db": {
"url": "${DATABASE_URL}",
"name": "db",
"connector": "postgresql",
"max": 100
}
}
explanation: your problem mainly like you're running out of connection pool, this will increase it. (100 is just a example number, you can change from 0 to the max number defined in your postgresql config)
Upvotes: 3