我正在使用续集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)
是什么使某些表不可交易?
答案 0 :(得分:1)
看来alerts
表正在使用does not support transactions的MyISAM
引擎:
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;