connection.query(`START TRANSACTION;`, async function (err) {
if (err) {
req.flash("flash", "Something went wrong while deleting. Try again.");
return res.redirect("back");
} else {
await connection.query(
`INSERT INTO... ; SELECT LAST_INSERT_ID();`,
async (error, results1) => {
if (error) {
await connection.query(`ROLLBACK;`, function (err) {
req.flash("flash", "There was an error while posting.");
return res.redirect("/post");
});
} else {
var post_id = await results1[0].insertId;
await connection.query(
`INSERT INTO...`,
[],
async (error, results) => {
if (error) {
await connection.query(`ROLLBACK;`, function (err) {
req.flash("flash", "There was an error while posting.");
return res.redirect("/post");
});
} else {
await connection.query(
`INSERT INTO...`,
async (error, results) => {
if (error) {
await connection.query(`ROLLBACK;`, function (err) {
req.flash("flash", "There was an error while posting.");
return res.redirect("/post");
});
}
await connection.query(`ROLLBACK;`, function(err){
req.flash("flash", "Went through...");
return res.redirect("back");
})
...
});
First I start the transaction. Then I have the code in the middle to insert data into 3 tables. I wanted to test the rollback which is supposed to undo everything after START TRANSACTION but it didn't do anything. What did I do wrong?
(The code works by itself so please ignore if I missplaced any brackets).