What is the best way to store a large object, such as a result set, across requests, but only for a short term, say for using the result set for printing for instance?

Alex Chaffee

[ I am assuming to store it in the session object would not be the best option. Also I don't want to have to re-issue the query just to print it or do something else with it. ]

First, as another poster pointed out, you should slurp the data from a ResultSet into an object (probably a JavaBean) immediately upon retrieval. That frees up database resources.

Then, what do do with the bean? You could store it in the session. Despite your skepticism, that's probably the best plan. The session will be around the next time the user makes a request.

If memory fills up, some servlet containers have the ability to swap the session data to disk; then, when the user needs it again, it will be automatically (and transparently to you) loaded back into memory. To assure this will work, make sure your bean implements the Serializable interface.

If the user gives up and goes away before using the data, then you needn't worry about memory leaks, since the session data will be released when the session expires.

Once you're done with the data, however, you should remove it from the session immediately, allowing normal garbage collection to take effect.

If the data set is too large to efficiently store into memory, then you can either serialize it to disk, or store it back into a database. However, those options require you to take responsibility for garbage collection -- you will need to have some algorithm for deleting old files or records -- and that can be a real pain in the neck.