1

I got a javascript client that sometimes sends two ajax requests within milliseconds of each other to the (php) server. (it's a javascript bug that I have no control over from the client side, i only got control over the server side).

The first request checks if a voucher already exists in the dbase (given a couple of parameters.. ie cust id etc).. if the voucher already exists, it just re-uses the voucher and updates is value, if it doesn't , it creates a new one from scratch.

the problem is that before it has finished checking if the voucher exists.. the second request comes in and checks if the voucher exists as well.. at that point the first hasn't created the voucher yet..

so long story short.. we end up with 2 duplicate vouchers.. (and the dbase doesn't restrict the voucher name to be unique. I have no control over the dbase either)..

so how do I prevent the second ajax request from doing anything until the first has done it's thing?

Keep in mind that the two requests are two different threads.. so if I make any $isVoucherCreationInProgress variables, it would be useless as the second call would be completely oblivious about it.

ideas?

9
  • 2
    You'd be better off with locks in the database - the first ajax request which reaches the server would lock the appropriate table(s), do its thing, then unlock. then the next request comes in, and tries to do the same thing. since the table's locked, the 2nd request will have to wait.
    – Marc B
    Mar 26, 2012 at 18:58
  • as mentioned in the question, i got no control over the dbase. I'm providing a plugin for an open source platform, and plugins can't modify core code/dbase
    – abbood
    Mar 26, 2012 at 19:01
  • Threads can share no info in php?
    – unludo
    Mar 26, 2012 at 19:15
  • not that i know of, if it could, please tell me how.
    – abbood
    Mar 26, 2012 at 19:23
  • you could use such a cache (see my answer below) : php.net/manual/en/book.memcache.php
    – unludo
    Mar 26, 2012 at 19:29

5 Answers 5

2

If the 2 ajax requests are from the same client, on the server side make a lock-like system, so if someone has checked for a voucher existence, set a session variable until it finishes what it has to do. So when the second line comes it first checks for the session and if ( someone else is using the voucher ) finish it with a message check later, so on the client side when it comes with a denied message you can simply send it with a 1 second delay, to be sure nobody is "working".

Hope this helps.

Or you could see this question in order to make a mutex in php: PHP mutual exclusion (mutex)

1

Personally I would think of a very simple method. On your server, you must have a procedure where you will create the voucher. Keep a global array and just before creating the voucher, set the index of array as the id, something just like key = > Value, where key may be the id of the voucher and Value may be a status such as "creating". After creating the voucher, you can remove the entry using the id of the voucher as the key.

Now, every time just before creating the voucher, simply check from the global array of the key already exist, if yes and Value="creating", then in fact, you are actually creating the voucher, so then you exits

Hope it helps, :-)

2
  • it doesn't.. as explained in the question above.. each thread has it's own scope.. so what's global to one is unknown to the other.
    – abbood
    Mar 26, 2012 at 19:05
  • 1
    the array can be in the session variable
    – Noor
    Mar 27, 2012 at 16:26
1

Use transactions. If you really can't touch the database (not even make your own statements), you can use STM or the like. Wouldn't be too hard with locks either, but either way requires that your application is running continuously. You can run a server with software like phpdaemon and forward a specific path to that server, to get that continuance.

4
  • 1
    Transactions won't protect two near-simultaneous request sequences. To prevent that, you need locks.
    – Marc B
    Mar 26, 2012 at 19:03
  • i don't have control over the dbase settings (i explain why in my answer to above's comment)
    – abbood
    Mar 26, 2012 at 19:04
  • @JanusTroelsen I'll look into that further, but at first glance I don't think locking/transactions would address my problem.. keep in mind that it's not about locking the first row in the dbase (for the voucher).. since the second call will simply create another row and not worry about the first (the voucher name is not a unique field nor can i make it as such).
    – abbood
    Mar 26, 2012 at 19:25
  • @abbood: The lock would lock whatever you want it too. What exactly are the properties that two simultaneous operations cannot share? Map those properties to locks and you can try and acquire that lock in each request. Mar 26, 2012 at 21:44
1

I understand that you create a new row in one table of your database.

You should add a unicity constraint so that you can't add it twice. Is it possible that you have to create several vouchers? Could you give more info on this?

Regarding the update, you should add a 'version' field to your row. The client side needs to have the correct version number to update the row. Thus it avoids a problem of unwanted concurrent update. This is a best practice with ORM, you may check this looking for 'optimistic update'.


As you have no control on the db, create a cache of requests (i.e. static object) in your server and create/update a row if nothing (regarding this customer + others parameters if needed) in your cache (like this one for example http://www.php.net/manual/en/book.memcache.php) . Your cache should clean itself atfer a while (I guess there are cache solutions in php).


Another idea (ugly but because it seems you are so limited with solutions): just make it slower. Wait sufficiently to make sure there is noone else (you will need a loop which checks and undo if needed - with random for convergence).

2
  • hey Unludo 1. I can't modify the dbase in way, shape or form. 2. memcache and other advanced caching is something that I got no control over either 3. performance is very important.
    – abbood
    Mar 26, 2012 at 20:47
  • the reason why I'm having so many constraints is b/c i'm offering a plugin that will be used by different merchants and environments.. so I can make very few assumptions about what infstracture they will have and what plugins/modulets etc they will have installed.. my solution has to be completely self contained (and light weight).
    – abbood
    Mar 26, 2012 at 20:49
0

You can either set a flag (in JavaScript) when your ajax request starts - check to see if it's set then RETURN, or you can change your AJAX request to synchronous.

3
  • I liked this answer the best for now.. It seems to work fine.. however when I look at the async description in the jquery page: asyncBoolean Default: true By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. this may be a problem b/c my client-side co-developers use jsonp and use cross domain
    – abbood
    Mar 26, 2012 at 19:13
  • 1
    @abbood you said that you have no control over the client side, so I don't understand...
    – unludo
    Mar 26, 2012 at 19:33
  • @unludo you are right.. and that's why i didn't close this issue yet (and even if I did, this may not resolve this matter as it has some serious side effects as i explained in my comment above)
    – abbood
    Mar 26, 2012 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.