0

Prerequisites

There is a lot of info out the about storing resources (images, videos etc) as blobs in database versus in filesystem with just the path saved in database.

Example:

https://www.quora.com/Difference-in-storing-image-in-database-table-vs-storing-image-in-folder-and-save-its-path-in-database-Which-is-better-and-why-PHP

I chose the latter part. The entity I'm saving in the database is sort of a tree like structure. An object in java consisting of other objects. These objects should be tied to folders in the file system.

Since the above mentioned objects consist of several parts I'm using transactions. I.e an all or nothing approach. So that either the whole object "tree" gets saved or if something fails, I make a rollback on the database.

The problem is I create the directory structure after this transaction. I'm thinking it might fail somewhat along these lines:

  • Thread 1 makes the transaction, and goes to the statement to create filestructure
  • Thread 2 makes a deleting transaction, removing the whole item Thread 1 is about to make a filestructure for
  • Thread 1 none the wiser now creates the filestructure of the entity now missing from the database

I'm doing this in Tomcat with MySQL as database, using mostly Servlets.

Problem:

Is there a proper or canon way to keep the file system and database entries "synchronized".

My thoughts:

I'm thinking of going from this:

transaction {
    addToDB(entity);
}
fileStructure(entity);

to this:

transaction {
    addToDB(entity);
    fileStructure(entity);
}

Thus putting the file structure creating process inside the actual transaction. And if need to rollback, then delete the folders created as well. Unfortunately since the code in the transaction to save the whole object tree structure is quite big (I don't use ORM), I'd rather not do that. Also it seems to break separation of concerns, since I would mix two different things in a code block, working with database as well as with the filesystem.

I checked these three links, but its either php or old or doesn't give much info, or its using Spring which I'm not. But maybe helps in understanding what I'm worried about.

How to manage transaction for database and file system in Java EE environment?

Saving file path to Database / SQL

Use transactions with both file and database

How would you go about it? Do you know of a standard way of doing this or do you have some good links that cover this? Because I can't find any. It's kind of funny that there is so much info saying, "save resource in file system and path in db", but not much dealing with the implications of that. That being "doing two things" that should still be in sync with eachother.

as a side note, I use the generated keys, primary auto incremented keys, as part of naming convention of the folders of an entity so as to make them distinct, then I need to first add them to db, to get those keys to then use in folder creation.

brat
  • 586
  • 5
  • 17
  • 1
    Already considered temp files? They get usually autodeleted on exit and you can simply move/rename them to permanent location after successful transaction. But then you've still the chicken-egg problem when that move/rename itself fails for some reason or probably the delete-from-db after that etc ;) – BalusC May 04 '21 at 14:23
  • Nope. But since it's a chicken-egg problem as you mention, I probably wont x). I have thought of synchronized methods in some Singleton FileManager. But then I cant be sure of the order in which threads operate on it. Plus could become a bottleneck? Then perhaps a queue of some sort, but then probably need to add to it from within the transactions. Afraid that could also easily become really complicated really fast. Maybe it's not a big issue and I'm just paranoid? I guess if I don't get any ideas I'll just settle for shoving FileManagment into the transaction code x( – brat May 04 '21 at 15:05

0 Answers0