Meep.
Well from what I gaher there is a anxiety issue there, which you have already said, but by you worrying about it and focusing on it it will probaly make it worse. On the times where it has not happened were you thinking abotu it?
Often an action is the result of a idea put in your head, if you had not thought abotu loosing your erection and began to worry about it, would it have happened, or by thinking about it did you put the idea in your head and cause it to happen.
Does the same happen when you receive oral sex?
I think you need to talk to your parnter about this. Maybe there are unerlying fears that you will let her down.
Viagra works in a few ways, physically it relaxes the prostate gland allowing more blood flow, mentally is where it holds most of its powers, men see viagra as the wonder pill, it turns them in to a super stud and the confidence they get from this thought often enough to help them get over their problem.
I know this may sound werid, but have you tried anal sex? According to my bf it feels very diffrent, maybe its the initial sensation of entering your partners lady bits that are the problem.
I agree talking can make a world of diffrence a sex therapist may be an idea.. (just be oprepared for the freudian ones!! They will bring your mother in to it no matter what!) or even your doctor, as it would be considered a mental health issue, you could go on the grounds of its makign you unhappy and depressed and depression is no longer laughed at these days.
I hope this helps, even just a little bit.
Best of luck