aspose file tools*
The moose likes JSP and the fly likes Paging search results and multiple browser windows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Paging search results and multiple browser windows" Watch "Paging search results and multiple browser windows" New topic
Author

Paging search results and multiple browser windows

Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Greetings,
I am using a tabular result set display binding tag library either/both of DisplayTag (Sourceforge) or DataGrid (Apache), and paging results (from a database). I can do all of this without any problems; the question I have is a question of result collection object scoping, and potentially multiple browsers open for a user session.

The problem/challenge that I have is that if I put the List/Collection object for the search result in session scope, and the user opens another browser (via ctrl-N, whatever), then any subsequent interrogation done in the new browser window will clobber the session scoped result object which was originally created by the first browser interaction.

Note: I'm assuming there is no clear way to page through a request scoped collection, because the request lifecycle has ended when the first page is rendered on the browser.

Also, to clarify, the session scoped result collection object contains the first up-to-500 rows of some ad-hoc database search/interrogation. On the search form, the user specifies some mandatory and optional search criteria, and a dynamic query is executed populating the (currently session scoped) collection with results. I *do not* want to requery the database (not even partial result set queries for pages/page-subsets) as the user pages paging through these potentially up-to-500 results (this is not an option, sorry).

At this juncture some EJB adepts might suggest "stateful session EJBs" are the solution, but I would prefer to avoid EJB altogether if possible. Maybe that's really the most natural approach though?

Anyway, if after searching and retrieving some initial results, but before paging to the 2nd page, suppose this user opens a new browser window (via ctrl-N, etc) and and performs a different ad hoc search/interrogation in this new browser, repopulating the *same* session scoped collection which the first browser window is supposed to be paging through. This has the effect of clobbering the first browser's results and if the user goes back to the first browser window and tries to page to the 2nd page, he/she will see results from the 2nd browser window search. My question is what's the best way to solve this dilemma.

What I'm hopefully kind of looking for is a general purpose technique/ approach/ standard-practice (hopefully strictly webapp, no EJB, if possible) which might allow users to page results concurrently in multiple browser windows. Thank you very much for reading.
Sincerely,
-Jim
[ September 14, 2006: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

The problem you are having is because the paging model you are using fetches more data than needs to be displayed and tries to cache it in the session. Hence the contention between multiple windows referencing the same session.

When paging datasets in my apps, I use a model in which only the data for the current page is fetched from the database. No caching, no contention, no worries.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
Yes, Yes, to OB1 you listen.

It's better to use the DBMS for paging. You can do this be modifying your query (e.g. where id >= 10 and id <= 20). Or you can use a database cursor if your database supports them. Also, several persistence apis have built in functionality for this, including JDBC in tiger). There is an abundance of info on the web. Just google 'jdbc paging'.


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
Rusty Smythe
Ranch Hand

Joined: Aug 09, 2006
Posts: 93

In addition to what the others said, let me add that you aren't using the session correctly; they way you are using it will greatly reduce the amount of scalability your application can handle.

Here's a handy guide:



Based upon this guide, you would put all of your query results into the request scope. This will help make your site more scalable and get you thinking more about memory management, which is a good thing.

Hope that helps.


* Here's how to tell if you have Enlightenment:

;P
[ September 14, 2006: Message edited by: Rusty Smythe ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

Originally posted by Rusty Smythe:

Based upon this guide, you would put all of your query results into the request scope.


I believe that his point was that he couldn't use request scope because the data for multiple "pages" that must exist across multiple requests are fetched at a time.

What database is being used? Databases like PostgreSQL, MySQL and Firebird have built-in SQL keywords that make it a snap to easily do the paging in the DB.
Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Thank you all for the feedback.
I natural to try to transform the problem into a simpler one.
However the original problem description stated:

I *do not* want to requery the database (not even partial result set queries for pages/page-subsets) as the user pages paging through these potentially up-to-500 results (this is not an option, sorry).

Even though this may *seem* irrational, a helpful suggestion for a solution to this problem will hinge on addressing this specific restriction. Maybe it would be helpful to understand the original question if you assume the database query takes 10 seconds regardless of whether it returns 10 or 500 rows. The database is unspecified. Thank you.
-Jim
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

If the database is "unspecified" how do you know it's going to take 10 seconds per query?

The solution is to fix the database problem, not try to code around it.
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
Value List Handler


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Please try not to dwell on the type of database or the duration of the query. 10 seconds was an exaggeration--I was just trying to give an example (admittedly not a very good one, sorry) of why someone might want to cache more than one page worth of results, somewhere (still TBD). Maybe it would have been better if had I left "database" out of the original question and replaced it with "very slow enterprise resource repository over which I have no say or control".
Back to the question (which do believe to be a difficult one), I would still very much appreciate any suggestions (other than query tuning as this is a constraint over which, again, I have no control, sorry)?
If there are no suggestions, perhaps someone might have suggestion(s) for how I can improve the framing/articulation of the original question, without transforming/changing it into a different one.
Thank all you very, very much for your patience with me--I do appreciate it. Really.
Sincerely,
-Jim
graham king
Ranch Hand

Joined: Dec 30, 2004
Posts: 133
you probably have to maintain two separate lists


write, revise, re-write, revise, again...<br />write, revise, re-write, revise, again...
Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Lynette,
Thank you very much for the "Value List Handler" pattern suggestion.
I must missed your reply before I posted my last one, thank you.
As I mentioned in my original question:
At this juncture some EJB adepts might suggest "stateful session EJBs" are the solution, but I would prefer to avoid EJB altogether if possible. Maybe that's really the most natural approach though?

Indeed, I just read it, and it looks like standard practice for "Value List Handler" is to use a stateful session EJB.
Supposing I did this, I'm still not clear on the standard mechanics of how the "Value List" for a particular browser window is looked up.
Would I use a hidden form field in my result page with some key in it and have the paging action do a (re)submit with this key? Is that what people usually do? Thank you for all of the help!!
Now I just noticed another response crept in (--thanks Graham (lover your BWK quote)!) which brings up more questions... Graham suggests maybe "two lists". Now I'm starting to worry about how I'm going to clean up these collections too. Is there a pattern for that? Oh my...
Also call me lazy (please), but is the Java Object Strategy outlined by the aformentioned link might suffer from missing code, unless I'm missing something now. Oh my... Thanks everyone!!
-Jim
graham king
Ranch Hand

Joined: Dec 30, 2004
Posts: 133
I started out using the VLI pattern with a SFB and after a long a painful journey into SFB hell I retreated and began using the HTTPSession. The request would have been ideal and I know one of these days I'll have to go back and rework it.

But for now it runs good because there's not an exhorbant amount of users trying to kill it each day.

I basically just followed what they had done and made some separate classes to display the results.

good luck
Kalpa Bar
Greenhorn

Joined: Sep 14, 2006
Posts: 1
Try AJAX:

1. Say, user opens a new window. Runs a new query. What you want is 2 objects in session stored for two windows. Unfortunately, I dont know much about AJAX/Javascript, so can not give concrete hint. You may have to keep window id etc. as key in session. Have some Javascript logic on page load (Javascript onload), generate/assign some window id, check in new window etc.

2. Or, you can use AJAX push technology (Google it). You are basically trying to refresh the content of a browser window from another browser window. Every time any of the opened windows gets new result, refresh all windows. All windows will ahve same data always.

This brings an interesting point: What is the purpose of http session? Is it to store all displayable data of say, 50 pages (each with 10 records or whatever). If your record size is small (just coupe of fields) and you have huge heap memory in JVM, and, not really many concurrent users, this is probably fine.
Sometimes, there are situations where data do not come from a SELECT SQL where you can use scrollable cursor etc., instead data is served by a stored procedure (not smart, but in reality, in lot of places there are things like this). Calling stored procedure repeteadly for every page of data may not make the programmer feel comfortable.

I think, for each page going to database is definitely better/safer idea to avoid this open-new-window issue.
Jim Hardin
Ranch Hand

Joined: Aug 02, 2006
Posts: 46
Thanks, Kalpa. I've offically bucked up and am willing to consider going to the database for every page. Thanks everybody!!
-Jim
 
 
subject: Paging search results and multiple browser windows