If this is standard upsert query in your app, you should turn it into a procedure and parameterize, catch exceptions anyway.
Or you could mess around with transaction isolation.