title: MySQL Alternative to "Lock Table" in Stored Procedure
body: <p>I have a requirement to insert a row into a table if a row does not already exist, and I need to do this using a stored procedure. I therefore need to select to check if the row already exists, followed by an insert. What I want to avoid is a race condition where 2 callers of the stored procedure may see that the row doesn't exist and then both try to insert the row.</p>
  
  <p>My first thoughts on solving this would be to lock the table, but unfortunately you cannot issue <code>LOCK TABLE</code> in a stored procedure.</p>
  
  <p>I want to avoid trapping a duplicate key insertion exception at the client (C#).</p>
  
  <p>Any way of locking the table or alternative solutions?</p>