posted 16 years ago
There's some magic you can employ if you're using a high quality DBMS and an ORM framework, such as Hibernate. And the best thing about it is that it actually makes your code simpler!
Traditionally, one might do a database read and stuff the results into a session object. That's a problem if you have lots of data and lots of users. You'll chew up a lot of server memory in a hurry.
However, if you use an ORM, records read can be cached (and they normally are), so that shared data doesn't get repeated for each user.
Additionally, on a smart DBMS, the DBMS server itself caches the query, so instead of reading everything in and using it later, you can simply repeat the query and the server will pick up where it left off instead of having to recompile the query, set up resources, and retrieve the records. This works best when you're using Prepared Statements instead of building up SQL manually.
ORMs normally take advantage of smart DBMS's, so if you use something like Hibernate, the first page can make the initial query, which returns a collection object. You can use the size() method to find out how big the result set is. If you detach the returned collection object and store IT in the session, the next page can re-attach it and retrieve data. That's what you'd normally do instead of actually redoing the query.
Some tweaking of this process may be needed, especially if others are adding or deleting records while this is going on, since the size returned won't be accurate unless you take a "snapshot" style query where the changes are ignored.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer