简体   繁体   中英

MySQL Rollback in node.js

I'm trying to issue the following in MySQL via node.js (using the https://github.com/mysqljs/mysql library).

I'm using the transaction API to rollback when an error happens but it doesn't appear to roll back. I next tried to simplify the problem and put it directly in PHPMyAdmin SQL box to do the following....

START TRANSACTION;
UPDATE users SET balance=balance + 1 WHERE id_user='someuserid'
ROLLBACK WORK;

I was expecting the user balance to remain at it's previous value (124) but instead it keeps adding one and shows an updated balance of 125 at the end of this.

Any idea what I'm doing wrong? Could it be the MySQL Db isn't supporting of transactions and/or is UPDATE like this allowed to be rolled back in transactions?

Ok, problem solved.

For reference for anyone else encountering this it was because of the table engine being used.

My table was using MyISASM which DOES NOT support transactions and fails silently. It autocommits on every transaction hence ROLLBACK never did anything.

The fix was to change the table engine from MyISAM to InnoDB (did via phpmyadmin but could also do it via ALTER TABLE table_name ENGINE=InnoDB; sql command.

Now the following works perfectly.

START TRANSACTION;
UPDATE users SET balance = 8 WHERE id_user='s10';
UPDATE users SET balance = 9 WHERE id_user='s12';
ROLLBACK;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM