I have multiple threads that save files on disk and put that information to DB.
On the other side of app I have multiple threads that read from DB this information and process mentioned files one at a time sorted by file_id:
SELECT * FROM files_to_process ORDER BY file_id
What I've invented is to make a PROCESSING_STATUS column which has four statusesNEW, PROCESSING, FAILED, SUCCESS.
Every worker is supposed to read ONLY one row from DB sorted by ID with status NEW and update immediately to status PROCESSING, so the other workers won't process the same file.
But, something tells me that I might end up with some race condition.
Will transactions solve this problem?
Unfortunately I can't make all operation inside transaction since processing files takes a lot of time and transaction pool will be exhausted, so I have to make two transactions in the following order.
- [In Transaction] Fetch row and update to status PROCESSING
- [No Transaction] Process File
- [In Transaction] Update final state to SUCCESSorFAILEDdepending on result
 
     
    