• Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance question

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Consider the following situation please :
In my web application, when the user click :
<link href="/bookDetails.do?isbn=123456789">Book Title</link>
The Action class gets the desired book from the database and puts it in the session scope, then it forwards the request to bookDetails.jsp
This page has Adding to shopping cart form.
My shopping cart is implemented as a HashMap.
My question is :
Do you suggest to declare my HashMap as :
HashMap<String isbn, Integer quantity>
Or:
HashMap<Book book, Integer quantity>
The first approach doesn't occupy alot of memory space but it will makes (maybe alot) database calls to retrieve the books when the user want to view his shopping cart.
While the second approach makes a good use of already loaded book but it may consumes memory.
What do you think ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Todd:
The Action class gets the desired book from the database and puts it in the session scope, then it forwards the request to bookDetails.jsp


Poor choice. if you are forwarding to the page, request scope is appropriate.


HashMap<String isbn, Integer quantity>
Or:
HashMap<Book book, Integer quantity>


Why send more data to the page than is necessary?

To me, this is isn't a performance question at all. The difference in performance is going to be negligible -- do what makes the most sense.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poor choice. if you are forwarding to the page, request scope is appropriate

Well, This problem is related to AddToCart Action.
This action gets the isbn from the request, put a simple message in the request scope in order to tell the user that the book has been added successfuly to his shopping cart and then it forwards the request to the bookDetails.jsp
Here is a simple work flow :
1. bookDetails.do (this forwards to) --->
2. bookDetails.jsp (this page has a link to addToCart.do) --->
3. addToCart.do forwards to bookDetails.jsp
So, if I'm using request scope, I will get a nasty Exception when bookDetails.jsp rendered after adding a book to the cart.
Do you suggest a better approach ?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though initally I also thought that using request would had been better but not anymore. Though it can be done I guess session would be better becasue :

A user can add any number of books and you need to keep a count of all the books. So either we go on populating a request attribute through a complex logic or we can simply put it in a session. As the user can move to any page after puting the books in the cart and then handling requests would be dificult.

As for the original question regarding performance. It depends how often would you need the whole of the Book object contents. In case you need the whole of Book contents quite a number of time I would suggest keeping the Book object otherwise just the String can be OK. Though a benefit of using a String would also be that in case between the transactions Book object is modified you would still get the latest.

As I think that your SQL server in any case would be doing some caching so the isbn would make more sense. But it all depends on your requirements.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Ajax may has a place here.
Put your book in the request scope and make your 'Add to cart' button call an Ajax code, so Ajax will update a portion of your page (lets say a span element), and every one is happy.
No more objects in the session scope, no more SQL calls.
Pretty, huh ? :roll:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!