mysql - Transactions in nodejs -
I have two questions that want to run inside the transaction rollback
var mysql = Need ('my sql'); Var connection = mysql.createConnection ({host: "host", user: "user", password: "pass"}); Var query1 = "Enter table 1 values (1,2,3)"; Var query2 = "update last_update set date = now ()"; Connection.beginTransaction {if (err) {error err;} connection.query (query1, function (mistake, result) {if (err) {connection.rollback (function () {err err;}); }};) Connection.query (query2, function (error, result) {iframe} {connection.rollback (function () {err err;}}}}})}};
When I run it, then there are no errors and no queries come out
If you start the transaction, then you also have to do it - and, nothing will be updated. You need to execute the following, but both questions After:
connection.commit (function (err) {if (err) {connection.rollback (function () {err err;});} Console.log ('Success! ');});
To know that after both questions, you can either put the second callback in the first callback, and the second in the callback, The problem is that by this you enter the so-called callback-hell
which makes your code basically unreadable.
How to avoid the callback hell beyond the scope of this answer, but just a look
Comments
Post a Comment