• Post Reply Bookmark Topic Watch Topic
  • New Topic

using ajax for long running query -- concept questions  RSS feed

Marsha Petry
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a long-running db query I'd like to do with Ajax and while it's running allow the user to do other things (submit). Once the query is done I never want to query again for that session (since it takes so long)

But I'm clearly missing a big concept... how do I handle a long running XMLHttpRequest query across a form submit? and then *not* do the query ever again?

If I were using JSP alone I'd do something like:
widgets = getWidgetsFromSession()
if widgets == null {
widgets = run long query to get widgets
save widgets in session
display widget list

Then on any page reload the widgets are taken from the session. Happy as a lark except for that darn initial waiting time. So to help with that wait time I want to try ajax:

/*load of page*/
kick off XMLHttpRequest to get widgets
display rest of page which has:
1. an hourglass to tell user widgets are being fetched
2. a form to allow file upload (or could be other action)

Now my confusion:
If I use XMLHttpRequest call a servlet that saves the queried widget list into the user's session, seems like I have to synchronize on the session because the user could have submitted the form, reloaded the page, and kicked off a 2nd XMLHttpRequest, which then calls the same servlet in a 2nd thread (right?). ouch! I don't want to sychronize session during a long query, but if I don't and the page reloads 3 times during the query, there are 3 new requests that may think they have to run the query (again).

If I don't touch the user's session in the servlet, then I've got a widget list in the callback and put in some inner HTML... how would I get that available to the page again after a form submit? Save it in a hidden var?

I'm advanced beginner in javascript and ajax, so clearly there are gaps in
my knowledge. But from what I read, a long-running query is a good use
of ajax.

Have I made any sense? and can you point me in right direction?

Thanks in advance,
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!