EateralZeng
EateralZeng

Reputation: 11

Operation consistency on both database and local file

There is a project that I'm developing the file upload/delete function. Every time when user uploads or delete file, it needs to operate database to insert or delete the file record.

This function support batch which means user can delete multiple files at one time. I put the database delete logic and local file remove logic in one java method with spring @Transactional. When any logic code throw exception, the whole method will rollback.

There comes the question, database delete logic works fine, during remove local files, it throws exception when removing the third one. Transaction works and database delete operation rollback, but the first two files which have been removed can't rollback. Has anyone some suggestion on this case?

BTW: database LOB type is forbidden in this project

Upvotes: 1

Views: 191

Answers (3)

Serge Ballesta
Serge Ballesta

Reputation: 148890

There are only two acceptable ways here:

  • either upload/deletion can be made transactional with the help of renames
  • or each file must have its own transaction (commit) in the database

You can make deletion transactional that way:

  • file to be deleted are renamed with a special prefix or suffix
  • deletion is noted in database in one single transaction
  • files are deleted

If there is a problem when writing the database, the application just renames the files with the original names. If there is a crash, you need a special processing on startup:

  • examine files that have the special name
  • search the database for their record
    • if present, the file must be deleted
    • if absent, the file must be renamed with the original name

You can imagine a similar processing for uploads...

Upvotes: 1

xs0
xs0

Reputation: 2897

Instead of deleting the files, you could first move them to a directory, where they're considered logically deleted. So upon error, you could move them back. After the transaction commits, you can then delete them for real, ignoring errors, and the worst thing that can happen is that they stay in the directory for deleted files..

It's still not 100% proof, though, and it's not clear that it's even possible - the OS and DB would have to cooperate in ways that are not available..

Upvotes: 0

Ravik
Ravik

Reputation: 734

Recovering deleted files from system is tricky and you would need to have understanding of operating system how it stores the files. Refer to this for details.

Upvotes: 0

Related Questions