2016-07-04 173 views
2

我與MySQL的後續交易問題(5.6.17),我有一個插入語句和兩個更新應該都完成或沒有,最後的高手transactions.create似乎回滾,但driver.update執行和不會回退和第三更新是trip.update聲明不作任何修改或回滾,控制檯掛起,幾秒鐘後,拋出此錯誤:Sequelize事務:ER_LOCK_WAIT_TIMEOUT

Executing (42a68c8e-8347-45af-b9a2-7b0e7a89606b): START TRANSACTION; 
Executing (42a68c8e-8347-45af-b9a2-7b0e7a89606b): SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; 
Executing (42a68c8e-8347-45af-b9a2-7b0e7a89606b): SET autocommit = 1; 
Executing (42a68c8e-8347-45af-b9a2-7b0e7a89606b): INSERT INTO `transactions` (`id`,`tId`,`total_price`,`company_share`,`driver_share`,`at`) VALUES (DEFAULT,'13',1000,100,900,'2016-07-04 10:44:43'); 
Executing (default): UPDATE `driver` SET `balance`=`balance` - 100 WHERE `id` = '1' 
Executing (default): UPDATE `trip` SET `paid`=1 WHERE `id` = '13' 
Executing (42a68c8e-8347-45af-b9a2-7b0e7a89606b): ROLLBACK; 
5---SequelizeDatabaseError: ER_LOCK_WAIT_TIMEOUT: Lock wait timeout exceeded; try restarting transaction 

交易部分是:

var Sequelize = require('sequelize'); 
var config = {}; 
config.sequelize = new Sequelize('mydb', 'root', null, { 
    host: 'localhost', 
    port: 3306, 
    dialect: 'mysql', 
    logging: true, 
    pool: { 
     max: 100, 
     min: 0, 
     idle: 10000 
    }, 
    define: { 
     timestamps: false 
    } 
}); 
require('sequelize-isunique-validator')(Sequelize); 
var driver = require('./../models/driver.js')(config.sequelize, Sequelize); 
var transactions = require('./../models/transactions.js')(config.sequelize, Sequelize); 
var trip = require('./../models/trip.js')(config.sequelize, Sequelize); 


return config.sequelize.transaction({isolationLevel:Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED},function (t) { 
return transactions.create({tId: tripId, total_price: totalPrice, company_share: companyShare, driver_share: driverShare}, {transaction: t}) 
    .then(function (result) { 
    return driver.update({balance: config.sequelize.literal('`balance` - '+companyShare)}, {where: {id: dId}}, {transaction: t}) 
     .then(function (result) { 
      return trip.update({paid: 1}, {where: {id: tripId}}, {transaction: t}); 
     }); 
}); 

}).then(function (result) { 
    RequestQueue.hmset(ticket,"ticketState",value.Paid); 
    res.json({'status': 'success','change':(-company_share)}); 
}).catch(function (err) { 
    global.console.log('5---'+err); 
    res.json({'status': 'failed'}); 
}); 

我m確定我的模型是正確的,因爲我在別處使用它們時沒有任何問題,並且不把它們放在這裏以保持問題的清晰和主題,但如果它有助於在評論中提問,tnx!

回答

3

您應該通過options對象內的transaction參數。

.then(function (result) { 
     return driver.update({balance: config.sequelize.literal('`balance` - ' + companyShare)}, { 
        where: {id: dId}, 
        transaction: t 
       }) 
       .then(function (result) { 
        return trip.update({paid: 1}, {where: {id: tripId}, transaction: t}); 
       }); 

http://docs.sequelizejs.com/en/latest/api/model/#update