In some project, sometimes I have found a similar requirement, cancel the execution of a process that take long time. For example, the user clicks in a button, this action trigger a process that calculate data an while the process is running, the user see a cancel button with a please wait message.
If the user clicks on cancel button, the transaction that was opened in the first request is marked as rollback and the execution of the process is stopped. With early technologies as EJB 3.0 or without an ejb container, sometime is difficult implement this logic to get the open transaction in a previous request from a new request to cancel it.
Please correct me if this solution is not appropriate for you, but I think that you could:
1. create an asynchronous business method (annotated with @Asynchronous) which will return Future<V> object,
2. client invokes the business method which runs in its transaction context,
3. because it's an asynchronous call, the client can still control the UI and can hit the "cancel" button which invokes Future<V>#cancel() operation,
4. the business method upon receiving cancel() operation, rolls the transaction back.
This is perhaps the answer to my recent prayers. I have a web app that sometimes must do a heavy database conversion. Now, it turns out that it's not possible to just do a POST request from the browser and wait an hour (or ten) for the response, since many browsers re-send the POST request after 15 minutes! (FF, IE, but not Chrome). Gaah! That would start another, parallelthread, with catastrophic result.
So perhaps the best practice in this case is to have the servlet use an @Asynchronous EJB 3.1 method and respond immediately? Of course, the completion can't easily be shown, but logging progress and completion in the application log file is sufficient for me - unless there's a better way?