我有一个使用回送和postgressql的nodejs项目。
我有一个类似的功能handleTrade
,只需以30秒的超时时间开始交易,按id查询交易并进行一些更新:
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();
}
}
我有一个10个ID的列表,我想异步处理所有ID,并在完成所有操作后打印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]);
这是日志:
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
在第1步之后,进程卡住了30秒,然后在事务超时时,它返回错误The transaction is not active
只有当我有10个id时,才会发生这种情况,如果我只使用9个,它将平稳运行
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
如果您有任何想法,请帮助我。谢谢
答案 0 :(得分:2)
尝试编辑您的数据源文件。
示例:我的dataresources.json
旧:
MyImage-641.png
新:
{
"db": {
"url": "${DATABASE_URL}",
"name": "db",
"connector": "postgresql"
}
}
说明: 您的问题主要是因为您的连接池用完了,这会增加它的数量。 (100只是示例数字,您可以将0更改为您的postgresql配置中定义的最大数字)