Skip Navigation

[Resolved] autoincrement type field with concurrent access

This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#1084075

Hi,
I created a system to book rooms with toolset and where users can log in and pay online. The system has also to generate a receipt when user pays and the receipt number should be sequential starting from 1. To do so, I created this function:
function get_reference_nb_func1($typeTransaction) {//to be called in code
$ref = get_post_meta( 643, 'wpcf-referencenb', true );
$ref+=1;
update_post_meta(643, 'wpcf-referencenb', $ref);
return $ref;
}
Whenever I generate a receipt, I call this function to increment the variable wpcf-referencenb. It works most of the time, however, when I noticed sometimes gaps in the numbers sequence, I guess because of the concurrency. Do you have a better idea to implement this with toolset?

Regards
Nabil

#1084502

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Nabil

Your implementation is simple, but I'm not sure if there is something in your code elsewhere that would account for the occasional gaps.

The code is rudimentary, it gets the current value, increments it, and updates the stored value.

To me the problem with site visitors triggering this more-or-less simultaneously is that you could end up with duplicates rather than gaps, e.g. one visitor triggers this code and gets a value of 17, increments it to 18, but before they update the stored value another user triggers the same code which gets the existing stored value of 17, increments it, and both update the stored value to 18.

I can't see a scenario where a gap would occur, but, as I say, you may have some code elsewhere that accounts for it.

In any case, it is not a Toolset thing as such, I'm not sure that I can help you much further with it.