I have experienced that behaviour you describe sometimes and it usually doesnt have to do with the database or the file.
Do you need a session or a identification so that the file can be downloaded? If so you have to bear in mind that some applications such as word or excel download the file themselves outside the navigator with their own http code and session. If that is the case the the cookie specifying the session is lost and so you are no longer logged from the program, resulting in that behaviour.