How to return a response to the user while keeping a JDBC transaction running in parallel?

I’m using play 2.6.

I need to do something like this:

1-Receive an HTTP request

2-Start a job to insert hundreds of thousands of rows in the Database, which might take hours to complete

3-Return an HTTP response immediately after starting the job

4-Keep the job running and send an email to the user when it’s finished

I found some documentation related to Akka https://www.playframework.com/documentation/2.6.x/JavaAkka but I don’t know if this would be it? Am I on the right path?

Hi.
I think that using actors for long processes like DB queries is not good idea. For these purposes it is better to use Future. But you can use actors for monitoring the end of the query (when a query is completed it sens message to an actor to change its state).
Read Don’t use Actors for concurrency to understand why you should do it this way.

1 Like

So, basically I could use a CompletionStage, set a timeout of like 1 second, return the response, and the job will keep running in the background?

To use CompletionStage, you do not need to set a timeout. What you can do is to run your DB query operation in a separate execution context (something you should do anyway unless your DB driver/lib is non-blocking already), and simply do not wait for the operation to return a result before you return the HTTP response.