how can i make sure only a single record is inserted when multiple apache threads are trying to acce

Posted by Ed Gl on Stack Overflow See other posts from Stack Overflow or by Ed Gl
Published on 2010-06-08T15:24:46Z Indexed on 2010/06/08 15:32 UTC
Read the original article Hit count: 189

Filed under:
|
|

I have a web service (xmlrpc service to be exact) that handles among other things writing data into the database. Here's the scenario:

I often receive requests to either update or insert a record. What I would do is this:

  1. If the record already exists, append to the record,
  2. If not, create a new record

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).

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.

Is there a solid way of handling this? I'm open to using locks if I can do it properly.

Thanks,

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql