How to write the following transaction code using async-await when using pool connection?
/* Begin transaction */
connection.beginTransaction(function(err) {
if (err) { throw err; }
connection.query('INSERT INTO names SET name=?', "sameer", function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
var log = result.insertId;
connection.query('INSERT INTO log SET logid=?', log, function(err, result) {
if (err) {
connection.rollback(function() {
throw err;
});
}
connection.commit(function(err) {
if (err) {
connection.rollback(function() {
throw err;
});
}
console.log('Transaction Complete.');
connection.end();
});
});
});
});
/* End transaction */
hi @ashokdey
This is how I think it supposed to work ( if not it's a bug ):
const conn = await pool.getConnection();
await conn.beginTransaction();
await conn.query('INSERT INTO ...', params);
await conn.commit();
conn.release();
I'll try to double check this later. Can you try as well?
hii @sidorares
it's working fine. I was close to it with a bug.
Thanks
Most helpful comment
hi @ashokdey
This is how I think it supposed to work ( if not it's a bug ):
I'll try to double check this later. Can you try as well?