Reputation: 11
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
Reputation: 148890
There are only two acceptable ways here:
You can make deletion transactional that way:
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:
You can imagine a similar processing for uploads...
Upvotes: 1
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