Storing images


I have a database with books and i 'm thinking to add image for each book. The question is how store these images :

Option A : In database
Option B : In a folder

I’m using CustomLayout for show the details of every book, so is easyer stored these images in a folder but i’ve 6000 o 7000 books…
It’s posible?

Which option is better?

Thanks in advance!

I think it is better to use folder for this. Handling BLOBS done right only in several databases, but if you’re using JPA, you probably will be unable to use BLOB features correctly anyway and it will end up loading every blob field into a main memory.

Storing 6-7K images in a folder is ok. You can (when creating an image for the first time) prepare proper illustration (crop, resize) and store the final png/jpeg file. Then use Vaadin’s FileResource to link the image to UI. So your heap will not be drained too much as with BLOBs.

But yes, from other side you’ll need to take care of backing up this folder together with your database backup. That’s a minus.

P.S. I recall there is a limit of 25K or so items in the same folder limit in ext3 fs in Linux. So if you expect to have such amount of images, you’ll also need to consider grouping images in a subfolders, say by date/month/year. This way you’ll not get out of 25K files per folder limitation.

Could it create the folder images under VAADIN/themes/mytheme/images…??


Consider how you deploy your application. Everything inside the WEB-INF, META-INF and VAADIN folders should be packaged in a War. Your application data and your images should survive a deploy/undeploy cycle, and so should NOT be under the application deployment hierarchy.

If you are using Tomcat, parameters like the location of the database and the location of your files should be in a context.xml or server.xml file, not in your war. All common containers have a similar notion (defining data sources, connection pools, resource locations) outside of the War, some of them also provide a Web-based interface.

That way you can have a test server and a production server with different databases and a different image folder, and you can use the same WAR for both (you really. really want to be certain that the WAR you tested is absolutely the same one as is in production, else your tests are worth absolutely nothing).

@Dmitri: foof, you scared me!
I have cache dir, where I can get more than 25k files, so I check it - 400k files in one dir - works fine :wink:

For the record:

Lot of pros and cons, so you can make your own idea (or be even more confused!).
Personally, I am not convinced that storing images in database is a win, in general.

Hi Andrew !
I think this also depends on the kernel compile options. At least I personally watched this effect on a CentOS 5.1 (or 5.2, do not recall already), where we had to split into several folders.