I don't understand whether you lack proper exception handling or you need to start a database transaction per mail-data-dump in your db.