Initiate a Transaction per Request #920
Replies: 1 comment 8 replies
-
A transaction is an atomic operation, so splitting it across multiple asynchronous handlers, though possible, is a difficult task. I was asked this before, I would always refer to poor system design. You should instead roll requests into some kind of internal state that can trigger a summary transaction once sufficient data is available, and share its result. This library however doesn't stop you from trying to split a transaction, but it doesn't provide anything extra for it. I remember seeing some library(s) trying to do this, can't remember their names presently, but you can and search for them yourself. Other than that, I won't be going into detail here on how to achieve it, for it would be quite time-consuming. |
Beta Was this translation helpful? Give feedback.
-
This is a general question more around "is this possible" and "how would I go about achieving this?"
My scenario is that I would like to initiate a transaction for every incoming request (I'm using nestjs, but that's bascially express under the hood I think) and then either commit or roll back based on whether any exceptions were thrown anywhere in my code.
The purpose is to retain transactional integrity across a complex system where many writes, from many services, could take place throughout the lifetime of each request.
I can see a solution involving interceptors and a global transaction being created but would welcome any input or suggestions.
I'll update this Discussion as my own research and investigation goes along, to assist anyone coming after me who may have a similar requirement.
Beta Was this translation helpful? Give feedback.
All reactions