• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

HashMap or database call

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have four drop down boxes with a one to many relationship with each other. For example: for drop down boxes, A, B, C, D. For each A, there are many values of B and for each B, there are many values of C and so on. Now, i'm confused on how to design it. I have come up with two options:

1. have hashtable AB with each value of A as a key and arraylist of B values corrosponding to A and so on. It does appear dirty to me as I'm going to have to filter unique A values and build an arrayList of B (right now, there are 2 rows in the table if A and B togther are unqiue, so A gets repeated if B is different). The advantage is no database trip.

2. Do a Database trip each time the value of A changes, get the values corrosponding to them and display them. I'm worried about database trip time, i can use Ajax call here so the whole page won't have to reload.

What do you guys think? Any preferences or any new ideas. I really appreciate all the help/suggestions i get.

Thanks,
Mark
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

You've correctly identified the issues. Caching something like this in memory is a gamble that ...

* You can afford the memory.

* The data won't change in the database, making your memory cache out of date.

* Getting all values from the database in one shot is actually faster than multiple small queries, especially if many of the values are never used.

Have you coded and tested this and proven that database access is a problem?
 
mark hunt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,

Thanks for the welcome and reply. No, i haven't coded it so don't know for sure. My concern is if it's a standard procedure to make a database call every time the user hits the page and selects from the drop down to populate the other box automatically. I'm leaning towards the database call as well...but just wanted to make sure it's the correct way of doing it..what do you think??
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last big system I was on had a utility that made simple queries on any database table. Drop downs and such use it, bypassing the regular "service" architecture. It has the option to cache query results and we left that on all the time. We figured the memory was worth the speed advantage of avoiding db calls.

Since you're doing AJAX you have the opportunity to do the very same thing in the browser. Retrieve drop down data through a JavaScript object that has the option to cache or make an XMLHttpRequest every time.

So with the Agile "Ya Aint Gonna Need It" and "Do the Simplest Thing That Can Possibly Work" concepts in mind, build with no caching but encapsulate the retrieval so you can add caching later if needed without changing all the code that uses it.

Oh, and I always leave myself a way to clear caches without recycling the server.
[ May 09, 2007: Message edited by: Stan James ]
 
no wonder he is so sad, he hasn't seen this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic