Here is the problem
A user of an enterprise web application is performing a task that results in a long (very long) database query (or other long processing intensiv
Show a message to the user that "Your request is accepted and will take an hour to get updated"
You create a table which stores all these transactions and process these in a batch on server.
User would not have to wait for long and he will be happy to see the message. You can send a confirmation email once your the transaction is processed.
This is the best solution I think.
The solution I have used before involves Jetty Cometd instead of AJAX. The main difference between Ajax and Cometd is that Cometd can uses more of a pub/sub model - the client (web browser in this case) subsribes to the publisher (your app) and the app pushes out updates and notifications to the web browser as apposed to an ajax model of constant polling of the server by the web browser. You can make use of the Cometd solution even if you are not using jetty - you can drop the jetty jars into the lib folder of your respective web server and you should be good to go.
I would do a combination of your so called "bad solution" and the "j2ee solution":
The trick is to match the request / response pair. I would do it like this:
NULL
as a value. Also pass that id to the UI.NULL
.This is clean and well abstracted from any concrete domain. A purely technical solution.
Even if you don't like polling, HTTP is stateless by design and I think this way polling only happens at well defined intervals.
Anyway, I implemented a system with exactly this pattern and it runs just fine...
How long running are these queries?
If you're talking about sessions expiring, perhaps it's best for the user not to be waiting around for it. It will always be annoying for the user to wait 20 minutes peaking every so often in that query's tab.
So, if really long queries are the case, perhaps it's best to change the UI approach and have the user "order" a query that he (she) later comes back to view, or is even notified by mail when it's ready.
In such a case I'd have the query prepared in the DB and cached, in a separate table, there. Meaning, I'd have the web server work once to register the request, have a DB task or a separate process/server prepare queries upon requests and notify the user, and then have the web server display them when the user comes back for the results.