I've made an application that saves uploaded files to the hard disk and saves additional data about the file in a database.
This is mainly done by a Struts-controller: saving to hard disk and by a Business Delegate, Session Facaede, EJB's the addition data is saved in de database.
My question is: how can i be sure that both action are done?
It is possible that only the file is saved to hard disk and the additional data not stored in the database opr visa versa.
Someone who can help?
In order to ensure both operations occur at the same time, you must make them transactional. This means (a) storing your file in a transactional file system and (b) performing the file save operation in a transaction monitor (that is, your EJB server).
This is very non-trivial, and there are simpler things you can do. You can "fake" the transactional operation in one of two ways:
1) Save the data in the database, and only save the file if the database operations succeeds. Since the file save operation is unlikely to fail, this will probably work.
2) Save the file, save the data in the database, then delete the file if the database operation fails. Since the delete operation is very unlikely to fail, this will probably work.
Note that (2) is a bit more robust, but is only a viable option if you do not have any other process that is manipulating the file. Also note that neither of theses are true transactions with 2-phase commits, but if you log all of the operations, you can probably recover manually from any of the (hopefully rare) failures.
Why not save the file content in the database too ?
Best regards, Mircea
it could be possible to save it in a database, but it would be harder to build a tree structure.
If you save the files on the hard disk, you can read each directory very easy.