Closed jmarkstar closed 4 years ago
You can't use a for loop like that with a future operation. What's happening is you're kicking off a load of saves then trying to return and release the connection whilst it's still being used by the saves. What you need to do is to capture the result of each save in an array and then flatten that array of futures before returning the status code
While that's true and would cause the transaction to be committed before the actual insertions succeeded, the crash is caused by something else.
Vapor 3's database drivers β with the exception of Redis which was rewritten from the ground up β expect you, the developer, to ensure that only one command is sent on a database connection at a time (instead of queueing them for example).
Therefore, using a for
loop (which starts all the requests "at once") will crash, and so would the flatMap(map(...).flatten(on: conn))
approach that usually works in other scenarios.
You have three options:
syncFlatten
method to execute the save operations one by one..wait()
on the save operations. The for loop can stay too, leaving you with easier to understand code.Unfortunately, I don't have time to show examples for either solution at this moment.
I tried this but I'm getting this error:
Fatal error: Attempting to call
send(...)while handler is still: callback(NIO.EventLoopPromise<()>(futureResult: NIO.EventLoopFuture<()>), (Function)).
Any help please.