r/PHPhelp • u/viremrayze • Jan 16 '25
concurreny problem while fetching a entry from the db
In my kyc project i assign a unique identifier, that i fetch from a different table with columan: id, boid_id, status, client_code, and timestamps. now i assign the boid_id to a particular client on a defined stage of the kyc journey after assiging a boid_id to the client i update the client's client_code to the boid table. the issue i am facing is that when two clients perform that step together a single boid_id gets assigned to two cleints. what could be the possible solutions for that( and i can't use the db facade to lock the db transaction), so suggest me a different approach.
Edit: following is the code for insta_boid assignment
if (empty($client->insta_boid)) { $insta_boid = NULL; $insta_boid_arr = InstaBoidMaster::where('status', '0')->orderBy('id', 'asc')->first(); if ($insta_boid_arr) { $insta_boid = $insta_boid_arr->insta_boid;
Log::info("insta boid assigned for");
Log::info($client_code);
Log::info($insta_boid);
$checkBOI = Client::select('insta_boid')->where('insta_boid', $insta_boid)->exists();
if ($checkBOI) {
$insta_boid_arr_new = InstaBoidMaster::where('status', '0')->orderBy('id', 'asc')->first();
$insta_boid_new = $insta_boid_arr_new->insta_boid;
$updateClient = Client::where(['mobile' => $mobile])->update(['insta_boid' => $insta_boid_new]);
InstaBoidMaster::where(['insta_boid' => $insta_boid_new])->update(['status' => 1, 'client_code' => $client_code]);
Log::info("insta boid re-assigned for");
Log::info($client_code);
Log::info($insta_boid_new);
} else {
$updateClient = Client::where(['mobile' => $mobile])->update(['insta_boid' => $insta_boid]);
if ($updateClient) {
InstaBoidMaster::where(['insta_boid' => $insta_boid])->update(['status' => 1, 'client_code' => $client_code]);
}
}
}
} else {
$insta_boid = $client->insta_boid;
}
$checkifBoidExistinaa = Client::where('insta_boid', $insta_boid)->where('client_code', '!=', $client_code)->exists();
if ($checkifBoidExistinaa) {
return response()->json(['status' => false, 'message' => 'Duplicate Boid Generated. Please Contact Support']);
}
$checkIBCount = Client::where('insta_boid', $insta_boid)->count();
if ($checkIBCount > 1) {
return response()->json(['status' => false, 'message' => 'Duplicate Boid Generated. Please Reach out to Support']);
}
View all comments
Show parent comment
1
u/Aggressive_Ad_5454 Jan 16 '25
OK, then, this is easy. Write a function that does what Eloquent / php code you you need with all that stuff to do about those boids. (Chirp chirp). If your code detects something wrong with that process throw an exception.
Then call it like this:
The magic of transactions, wrought by two generations of lonely code and testing magicians who work on databases, will do everything right and without race conditions.