简体   繁体   English

Node.js中的MySQL回滚

[英]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). 我正在尝试通过node.js在MySQL中发出以下命令(使用https://github.com/mysqljs/mysql库)。

I'm using the transaction API to rollback when an error happens but it doesn't appear to roll back. 当发生错误但我似乎没有回滚时,我正在使用事务API进行回滚。 I next tried to simplify the problem and put it directly in PHPMyAdmin SQL box to do the following.... 接下来,我尝试简化问题,并将其直接放在PHPMyAdmin SQL框中以执行以下操作。

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. 我原本希望用户余额保持在以前的值(124),但它会继续增加一个,并在此末尾显示更新后的余额125。

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? 难道是MySQL Db不支持事务和/或这样的UPDATE是否可以在事务中回滚?

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. 我的表使用的是MyISASM,它不支持事务并且无提示地失败。 It autocommits on every transaction hence ROLLBACK never did anything. 它会在每个事务上自动提交,因此ROLLBACK不会做任何事情。

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. 解决方法是将表引擎从MyISAM更改为InnoDB (通过phpmyadmin进行,但也可以通过ALTER TABLE table_name ENGINE=InnoDB; sql命令来完成。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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