node.js - would mysql js pool cause transaction deadlock -
i designing ticket system using nodejs , mysql-promise.
safety , consistency, want use transaction worry deadlock.
scenario
- people can buy multi tickets in 1 order
- i check every tickets whether rest amount of tickets enough or not.
- i update amount of tickets
- i create order
the code
await ctx.conn.begintransaction() // check amount of tickets let ticketidlist = ctx.body.tickets.map(t => t._id) let ticketlist = await models.ticket.findticketlist(ctx.conn, ticketidlist) (let t in ticketlist) { let ticketbook = ctx.body.tickets.filter(tb => tb._id === t._id) if (t.perminsalecount > ticketbook.count || t.permaxsalecount < ticketbook.count) { ctx.stats = 400 return ctx.body = { error: "failtickets" } } if (t.sold + ticketbook.count > t.total) { ctx.stats = 400 return ctx.body = { error: "soldout" } } } // update tickets await promise.all( ctx.body.tickets.map(t => models.ticket.reserveticket(ctx.conn, t._id, t.count)) ) // create order await model.order.createorder(...) await ctx.conn.commit()
my big concern ctx.conn create pool
mysql.createpool({ ...... })
would // await promise.all(ctx.body.tickets.map(t => models.ticket.reserveticket(ctx.conn, t._id, t.count)))
lines make sql in multi connections , cause deadlock ?
how use conn.begintransaction()
?
thanks
Comments
Post a Comment