I have been banging my head over this for awhile now and google's no help. so any suggestions would be greatly appreciated.
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... response.setContentType("image/jpg"); response.getOutputStream().write(rb,0,len);
I then call this servlet from the image src tag like so <img src="ImageServlet?id=123"...> 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.
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?
Option 1) A new servlet takes 20 image IDs and caches them somehow(?).
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?
This would involve either saving the files in user session
If the images aren't user-specific, the session would be a very poor choice.
Option 2) Preload images in the jsp. Im not sure if this can be done
If by this you mean grab the bytes and hope to be able to do something with them, then yeah, dead end.
So there you have it. If any gurus out there have experience with showing multiple images from a database
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.
At least repeat visitors will fetch the images out of their browser caches (assuming you structure the image URLs and response headers properly).
Thanks Bear, 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?
Joined: Jun 20, 2008
We finally came to a middle ground solution. Since only thumbnails are shown in the search results page these will be served from an external file system. The actual image will still be served from the db one image per page. This ensures files get backed up along with the db backup and the thumbnails can be regenerated from them incase of a filesystem failure. 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.
subject: Display multiple images in JSP from MySql