Know How to Handle Large Queries
One of the biggest performance problems with entity beans is doing queries on data that return large collections of objects. This usually happens, for example, when you need to display a list of items to a user, each corresponding to a domain object handled by an entity bean. Simply using a finder method and then getting value objects from the returned entity beans will not work very well, especially if you need this data in the read-only form just for display purposes.
A good solution is to bypass entity beans entirely and query the database directly. You can have a stateless session bean that is responsible for query operations, and it can internally do a database query and return a list of plain value objects. A problem with this scheme is that the database records might change while the client is still using the query results. This is an accepted drawback of read-only query methods, and there is nothing you can do about it.
To see how this querying scheme might be implemented, suppose you
have a User value object that contains name and
country fields. Example 2-4 shows how to implement
your stateless session bean.
public class UserQueryBean implements SessionBean { // Skipping EJB methods public LinkedList findCanadianUsers( ) { // Do a SELECT * FROM USERS WHERE COUNTRY='Canada' ORDER BY ID // and get a result set back. return createListFromRS (rs); } protected LinkedList createListFromRS (ResultSet rs) throws ...