Play framework long running jobs
I have some play jobs that perform long actions (say 10 seconds).
Some of wh开发者_StackOverflow中文版at they do needs a db transaction and some don't.
Since the entire job is a single transaction, I will have long open db transaction, which is not a good practice.
What is the best way to divide the job to several transactions? Should I use JPAPlugin closeTx and then startTx?
I don't think the long running db query is such a bad thing. What is bad however is that the http thread will be blocking other threads during this time.
Therefore, I would suggest looking into Futures, and also using the suspend method, so that the http request can be free'd up, and you can check if your future task has been completed at regular intervals without blocking other incoming http requests.
Also, in Play 1.2 (which should become available for beta soon), it has much better support for FutureTasks and delaying responses back to the browser.
by job I assume you mean a Job, not a HTTP request.
If what you are doing is using a request to trigger some long task, try to break the task into a synchronous and asynchronous parts, so you return to the user quickly (and close the request transaction) and then the asynchronous part (via Job or Future) will tackle the rest. It may require some compensation-code in case the asynchronous task fails, but that's ok.
If what's taking long is an asynchronous job and you are using many entities, try to break it into smaller jobs. Do something like a queue of tasks stored in a table and a job that runs every few seconds and does only the first task. That way you will not have the issues related to long transactions.
If you can't break the task (unlikely, but could happen) then there is no point in worrying about it :)
精彩评论