I see a couple of issues with this:
1) This may not scale well; if select_data becomes large you may be spending a while in the dtml-in loop.
I would write the select_data query to only select a single record, i.e. the one I'm worried about duplicating.
2) There could still be a race condition here, between the check for data and the insertion (especially coupled with the above point - the probability would increase proportionally with the load of the machine and the size of the table). I say "could" here (guardedly :-), because I don't know how Zope's transaction semantics are implemented, and how they affect the DA that you're using (does anyone have any pointers to documents? I've just started reading "How to Write a DA", and this mentions the distributed commit protocol).
I'm not sure if a solution to the original problem is to surround the insert with a <dtml-try> tag; I'm going to try this myself sometime today (if I get some spare time :-).
Good point. Yours is probably a better solution. <dtml-try> <dtml-call insert_data> <dtml-except specific_exception> <dtml-var error_message> </dtml-try> --jfarr