File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes More Roundtrips or More Data (opinions plz) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "More Roundtrips or More Data (opinions plz)" Watch "More Roundtrips or More Data (opinions plz)" New topic

More Roundtrips or More Data (opinions plz)

Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Hi all,
So, the inevitable question has arisen -- more trips to the database, or more java objects to encapsulate the data. Suppose I have a class Employee, and a web application which provides an interface to manage Employees. What are your thoughts on:

  • initialize a List of Employee objects, populating each one with data from the database, and allow user to navigate through List, make modifications, etc.
  • initialize one Employee object (the Employee currently being viewed) and as the user selects another, retrieve data from database and update Employee object with data

  • The likely number of users at any given time is less than 100, and the number of Employees in the database is less than 500. What do you think? Thanks!
    Bear Bibeault
    Author and ninkuma

    Joined: Jan 10, 2002
    Posts: 63844

    The number of users will not affect the memory footprint (unless the list is customized on a per-user basis), and with such a small number of objects, I do not see any issues with caching them in memory as long you understand the nuances of caching (like how do you fault items out of the cache when they are updated in the database and so on).

    [Asking smart questions] [About Bear] [Books by Bear]
    Jeffrey Hunter
    Ranch Hand

    Joined: Apr 16, 2004
    Posts: 305
    Well, I lost the argument for populating all the Objects, thus incurring only one roundtrip to the db. We decided to conserve memory, since each user will, in fact, have a customized list of Objects at their disposal, and this may lead to some memory overload (I guess, but I still don't think it is significant). Instead of populating a List of Objects, we populate one Object, and have a List of "scaled-down" Objects (with only a few key attributes for lookup in the db). When the user chooses to examine another Object, we look up the scaled-down Object attribute, send the query to the db, and retrieve the complete Object and dump it in the Session. In a nutshell, look at this pattern:

    So, the Employee class represents the complete model of an Employee from the database. The EmployeeIndex represents only the info we need to lookup an Employee in the database (I'm not sure there should even be an inheritance relationship here). Essentially, a List of EmployeeIndex objects will be displayed on the jsp page and, in response to a click, the respective Employee object will be retrieved from the database.

    So, it seems to me, the only savings we have is that the EmployeeIndex class does not have all the additional attributes of a full-blown Employee, and thus has a smaller footprint in memory. What do you guys think?

    I agree. Here's the link:
    subject: More Roundtrips or More Data (opinions plz)
    It's not a secret anymore!