Node.js + MySQL - handling transactions

前端 未结 5 1374
慢半拍i
慢半拍i 2021-02-07 22:27

I am building an app on node.js using express, and node-mysql driver. There is a couple of cases in my app when I need to make a series of database inserts/updates. I want them

5条回答
  •  滥情空心
    2021-02-07 23:07

    You'll need to create a client pool, or somehow otherwise ensure that two different pages aren't interspersing commands on the same connection (at least while any of them is in a transaction).

    Since you want to conditionally do a rollback based upon the result of an earlier command, you'll need to chain the db calls together through their callbacks and not rely on the node-mysql queuing behavior. That will open up a window for some other page to come in and queue up an operation on the same connection as you suggest.

    You could create and manage your own queue, but that would end up serializing all transactional pages (assuming you're sticking with the single connection model).

    From a quick googling, it looks like there are several node-mysql pools on github. After looking at them, though, they don't look like they'll help with your issue.

提交回复
热议问题