MySQL ROLLBACK没有删除新记录 - 续集

问题描述 投票:0回答:1

我正在使用sequelize v5.1.0在MySQL 5.7.25-0ubuntu0.18.04.2中创建一个事务。它似乎是根据MySQL 5.7 Documentation执行正确的命令,但是,之后插入和回滚的记录仍然存在于数据库中。

JavaScript的

let promises = []
models.sequelize.transaction(function (t) {
  promises.push(models.alert.create(setter, { transaction: t }))
  promises.push(new Promise((resolve, reject) => {
    reject(new Error('roll it back yall'))
  }))
  return Promise.all(promises)
}).then(function () {
  console.log('SUCCESS!!! (will commit)')
}).catch(function (err) {
  console.log('FAILURE !!! (will rollback)')
  next(err)
})

SQL查询日志

| 2019-03-21 12:55:17.798200 | root[root] @  [10.211.55.2] |      2151 |         0 | Query        | START TRANSACTION                                                                                                                                                                                                                                                                         |
| 2019-03-21 12:55:19.597304 | root[root] @  [10.211.55.2] |      2151 |         0 | Prepare      | INSERT INTO `alerts` (`id`,`user_id`,`alert_name`,`reading_type`,`reading_condition`,`reading_value`,`always_active`,`sensors_global`,`enabled`,`last_updated`,`updated`) VALUES (DEFAULT,?,?,?,?,?,?,?,?,?,?)                                                                            |
| 2019-03-21 12:55:19.616278 | root[root] @  [10.211.55.2] |      2151 |         0 | Execute      | INSERT INTO `alerts` (`id`,`user_id`,`alert_name`,`reading_type`,`reading_condition`,`reading_value`,`always_active`,`sensors_global`,`enabled`,`last_updated`,`updated`) VALUES (DEFAULT,21,'Test Alert','temperature','below',60,1,0,1,'2019-03-21 12:55:17.781','2019-03-21 12:55:17') |
| 2019-03-21 12:55:19.619249 | root[root] @  [10.211.55.2] |      2151 |         0 | Query        | ROLLBACK

记录在数据库之后

mysql> select * from alerts where alert_name='Test Alert';
+-------+---------+------------+--------------+-------------------+---------------+---------------+---------------+----------------+---------+---------------------+---------------------+
| id    | user_id | alert_name | reading_type | reading_condition | reading_value | alert_message | always_active | sensors_global | enabled | updated             | last_updated        |
+-------+---------+------------+--------------+-------------------+---------------+---------------+---------------+----------------+---------+---------------------+---------------------+
| 48689 |      21 | Test Alert | temperature  | below             |         60.00 | NULL          |             1 |              0 |       1 | 2019-03-21 06:55:17 | 2019-03-21 12:55:18 |
+-------+---------+------------+--------------+-------------------+---------------+---------------+---------------+----------------+---------+---------------------+---------------------+
1 row in set (0.00 sec)

更新:在MySQL CLI中进行调整会发出警告:

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into contacts ( user_id, contact_name ) values (21, 'Some Person' );
Query OK, 1 row affected (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+---------------------------------------------------------------+
| Level   | Code | Message                                                       |
+---------+------+---------------------------------------------------------------+
| Warning | 1196 | Some non-transactional changed tables couldn't be rolled back |
+---------+------+---------------------------------------------------------------+
1 row in set (0.00 sec)

是什么让一些表非事务性的?

mysql node.js sequelize.js mysql-5.7
1个回答
1
投票

似乎alerts表使用MyISAM引擎,does not support transactions

mysql> show table status like 'alerts';
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| Name   | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation       | Checksum | Create_options | Comment |
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
| alerts | MyISAM |      10 | Dynamic    |   18 |            136 |        2712 | 281474976710655 |         2048 |       256 |          48690 | 2019-03-19 10:38:39 | 2019-03-21 06:55:19 | NULL       | utf8_general_ci |     NULL |                |         |
+--------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------------+----------+----------------+---------+
1 row in set (0.00 sec)

要更改数据库引擎,请遵循以下指南:here和:

mysql> ALTER TABLE alerts ENGINE=InnoDB;

© www.soinside.com 2019 - 2024. All rights reserved.