Closed ghost closed 5 years ago
I think you'll be needing a request per item in your array. You're running multiple queries on the same request before waiting for the previous one to finish:
for(let TEST_ID of array){
let db_insert = 'INSERT INTO T_TEST_TABLE VALUES(' + TEST_ID + ');'
request.query(db_insert, (err, result) => {
Closing as not a defect with the library.
sorry. I do not understand well.
How can I finish a "query"?
What I want is to execute multiple "queries" and "rollback" them when an "error" occurs.
I want to make it like "proceduer" in "mssql".
sorry. I'm a Korean developer and I'm not good at English.
You need to create a new request per query in the loop. You can still pass in the transaction to the request.
Bear in mind query execution is asynchronous so you're committing the transaction before all queries have a chance to complete.
https://github.com/tediousjs/node-mssql#query-command-callback
Thank you so much! I'll change "source" and write "COMMENT" again!
willmorgan!! Thank you so much!
Using "async.eachSeries" instead of "for" statements makes it synchronous and works well.
You are my benefactor best regards!!
Here is the code changed as follows.
test_db_insert()
function test_db_insert(){
const pool = new sql.ConnectionPool(config)
pool.connect(err => {
if(err) console.log(err)
const transaction = new sql.Transaction(pool)
const request = new sql.Request(transaction)
transaction.begin(err => {
if(err) return console.log(err)
const array = [1, 2, 3]
// for(let TEST_ID of array){
async.eachSeries(array, function(TEST_ID, callback){
if(TEST_ID == 3) TEST_ID = 1 //Deliberately "error"
console.log(TEST_ID)
let script_inc_insert = 'INSERT INTO T_TEST_TABLE VALUES(' + TEST_ID + ');'
request.query(script_inc_insert, (err, result) => {
if(err){
callback(err)
}else{
callback()
}
})
}, function(err){
if(err){
transaction.rollback()
console.log(err)
}else{
console.log('success!')
transaction.commit()
}
})
})
})
}
Expected behaviour:
transaction.begin & loop DML excute error Can't reuse "request"?
error log :
Actual behaviour:
Configuration:
Windows 10 Pro X64
Software versions