[英]Managing connection pool issue when updating several rows in oracle db with node.js and knex.js
我正在使用 Node.js 應用程序,它在 Oracle 數據庫上插入和更新數據。 有一種情況,我需要在單個 API 調用中為行數更新表的列值。 但是,觀察到這是一項耗時的任務,有時會達到連接最大池大小,從而出現以下錯誤。 增加池大小也無濟於事。
Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
為了緩解這個問題,為所有更新創建一個事務,並使用 Bluebird.js 以並發方式執行更新調用,如下所示。
knex.transaction((trx) => {
...........
...........
Bluebird.map(allObjects, (item) => {
.......
trx('Table_Name')
.where('ID', item.ID)
.update({abc: JSON.stringify(updatedAbc)})
........
},
{ concurrency: 10 }).then(()=>{ trx.commit });;
然而,這也給出了上述問題。 連續調用端點時,出現高於池大小的問題。 有沒有更好的方法來解決這個問題。 非常感謝幫助。 謝謝你。
您至少缺少一些 return 語句。 也沒有理由嘗試在事務中同時更新多行,因為在事務中所有查詢都是按順序執行的,如果有人嘗試做類似的事情,驅動程序只會將它們排隊。
// more or less fixed version of the code above:
knex.transaction((trx) => {
return Bluebird.map(d, (item) => {
return trx('Table_Name')
.where('ID', item.ID)
.update({abc: JSON.stringify(updatedAbc)});
}, { concurrency: 10 }).then(() => console.log("transaction completed with success")).catch(err => console.log("Transaction was rolled back", err)));
更好的方法:
// throws an error if transaction did fail and was rolled back
await knex.transaction(async (trx) => {
for (let item of allObjects) {
await trx('Table_Name').where('ID', item.ID)
.update({abc: JSON.stringify(updatedAbc)});
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.