The question I ask needs some introduction. I have an application that imports small files and e-mails into its own SQL Server database from a folder every 5 minutes. It is of great importance that
- each file must be imported
- each file must only be imported once
Now part 1 is not a big deal. Part 2 however raises some concerns. If the file cannot be deleted after being successfully imported (for example due to lack of access rights), and the next time my application looks at the folder the file will be imported again and again, until someone fixes the access rights problem.
Thus I was wondering if the following approach would work safely, without data loss:
- start a transaction
- import the file into the DB
- delete the file from the folder
- commit the transaction (or rollback if there was an error)
This is the point where I arrived to the title of the question: will the last commit step always succeed if prior steps did not throw an exception? Am I safe to delete the file before the commit, without risking losing the file? What if SQL Server is shut down during transaction? Or shall I delete the record from the database if file deletion failed?