Another way to do this is to create the form and fields through HTML and post this HTML form to an agent. This way you are not actually saving a document on the submit, you are just posting the data to the agent. It is then up to your agent, as to how you process the data. This way you can 100% guarantee that the record will only be processed, once the agent runs successfully.
From your code, you appear to be running Java for your agent. With the above solution, you could implement a Java Servlet to handle all the data.