Running into a race condition where a newly created object cannot be immediately fetched outside of a database transaction
OultimoCoder opened this issue · 1 comments
OultimoCoder commented
Using this code:
const transactionId = await db.transaction().execute(async (trx) => {
const insertId = await insertTransaction(data, trx)
await doSomething(trx)
await doSomething2(trx)
await doSomething3(trx)
return insertId
})
const transaction = await getTransactionById(transactionId, db)
if (!transaction) {
throw new ApiError(httpStatus.BAD_REQUEST, 'Created transaction not found')
}
return transaction
I have a 80% success rate to return the transaction. The other 20% of the time the transaction is created but it returns an error that it can't find it.
Obviously, I could fix this by putting the fetch inside the database transaction but then my test becomes flakey instead.
Surely, if I await the database transaction I should 100% of the time be able to fetch it afterwards no issue?
OultimoCoder commented
Ahh my bad I was on version 1.1 I see this was fixed in an update