We save image thumbnails in a MySQL Database. Please,please do not tell me to switch to a filesystem as this decision was made after a lot of gut-wrenching debate and we are well into the dev cycle to change design.
I wrote a simple servlet that when passed the image ID fetches the blob and streams it to the http response stream:
byte rb = ...call db and stream blob to byte array here...
I then call this servlet from the image src tag like so
Everything works fine so far.
The requirement is now to display multiple thumbnails on a single page(Say 5 rows of 4 thumbnails each)
If i use the above setup then the page would fire multiple(20!) queries to fetch all the images. This is what i wish to avoid and where iam stuck. My alternatives:
Option 1) A new servlet takes 20 image IDs and caches them somehow(?). This would involve either saving the files in user session or writing to webserver which would quickly overwhelm resources when 100s of users request 1000s of pages.
Option 2) Preload images in the jsp. Im not sure if this can be done. need to write some test code and see.
So there you have it. If any gurus out there have experience with showing multiple images from a database then please do share your experience. Any insights/suggestions/links/critiques welcome.
Meanwhile, I will keep looking and keep you posted on my findings.
Mark in the Dark
Originally posted by mark walter:
If i use the above setup then the page would fire multiple(20!) queries to fetch all the images. This is what i wish to avoid ...
Why? Are you trying to solve a real problem, or an imagined one? Are you fearful of the network traffic or DB load?
If you find that banging on the database leads you to do this, maybe it is time to revisit the database decision, gut-wrenching as it was. If performance wasn't a consideration, what considerations were used to determine to use a DB rather the filesystem?
Option 1) A new servlet takes 20 image IDs and caches them somehow(?).
If the images aren't user-specific, the session would be a very poor choice.
This would involve either saving the files in user session
If by this you mean grab the bytes and hope to be able to do something with them, then yeah, dead end.
Option 2) Preload images in the jsp. Im not sure if this can be done
The only time I tried to use images from a DB, I backed away from that decision simply because of the DB load it created. But if your decision has been irrevocably made, despite these issues, then you're kinda stuck with some sort of caching strategy.
So there you have it. If any gurus out there have experience with showing multiple images from a database
At least repeat visitors will fetch the images out of their browser caches (assuming you structure the image URLs and response headers properly).
The DB vs. FileSystem argument really seems to go nowhere. I would gladly debate the pros and cons on another thread if you wish.
As for the current issue, yes, the load is one reason why I would look for a better approach then sending multiple requests which would fire multiple queries to the db.
Anyway, thanks for validating my suspicions about the second Option.
I guess I need to look at different caching alternatives as you suggested.
any other inputs? anyone?
External files need to be handled by a servlet so that users cannot access the files directly. The servlet can handle the FileIO and stream the image contents to the response.
Well, hopefully that takes care of our image management issues. Hope this helps someone else.