title: how can i make sure only a single record is inserted when multiple apache threads are trying to access the database?
body: <p>I have a web service (xmlrpc service to be exact) that handles among other things writing data into the database. Here's the scenario:</p>
  
  <p>I often receive requests to either update or insert a record. What I would do is this:</p>
  
  <ol>
  <li>If the record already exists, append to the record,</li>
  <li>If not, create a new record</li>
  </ol>
  
  <p>The issue is that there are certain times I would get a 'burst' of requests, which spawns several apache threads to handle the request. These 'bursts' would come within less than milliseconds of each other. I now have several threads performing #1 and #2. Often two threads would would 'pass' number #1 and actually create two duplicate records (except for the primary key). </p>
  
  <p>I'd like to use some locking mechanism to prevent other threads from accessing the table while the other thread finishes its work. I'm just afraid of using it because if something happens I don't want to leave the table locked. </p>
  
  <ul>
  <li>UPDATE: 
  The table already has a primary key. The ideal situation should be that the first thread should create the record if it doesn't exist, then once the second thread comes in, it won't create the another record, but just update the record that was already created. It's almost as though I'd like to make the threads form a single-file line.</li>
  </ul>
  
  <p>Is there a solid way of handling this? I'm open to using locks if I can do it properly.</p>
  
  <p>Thanks,</p>