【发布时间】:2019-03-21 23:15:53
【问题描述】:
我正在使用 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