我需要在一个事务中执行3个不同的更新语句。我正在使用node-oracle包。是否有一个示例显示如何进行交易?
答案 0 :(得分:0)
最初,您可以将autoCommit
设置为false,一旦完成任务就可以提交它。
通过使用连接功能connection.commit(function(error))
示例:
var oracledb = require('oracledb');
oracledb.autoCommit = false;
并且成功执行connection.execute()时,可以按以下方式提交它
conn.execute(
"INSERT INTO test VALUES (:id, :nm)",
[2, 'Alison'], // Bind values
function(err, result) {
if (err) {
return cb(err, conn);
} else {
console.log("Rows inserted: " + result.rowsAffected); // 1
conn.commit((error)=> {
console.log('Error : ', error);
});
}
});
答案 1 :(得分:0)
There are examples in the node-oracledb GitHub repo. You might prefer to look at the dev-4.0 branch which has been updated to use Node.js 8's async/await style, making it easier to understand and get correct (but don't forget to use await
). Also read the manual.
The example you want is insert1.js which shows 3 statements forming a transaction. The first two statements don't commit but the last uses autoCommit
(which saves the cost of an explicit commit()
):
result = await connection.execute(
`INSERT INTO test VALUES (:id, :nm)`,
{ id : {val: 1 }, nm : {val: 'Chris'} });
result = await connection.execute(
`INSERT INTO test VALUES (:id, :nm)`,
[2, 'Alison']);
result = await connection.execute(
`UPDATE test SET name = :nm`,
['Bambi'],
{ autoCommit: true }); // commit once for all DML in the script