Today I wrote 2 of the same codes in laravel and simple PHP without a framework to check race conditions. In simple PHP code, race condition happens when I send requests concurrently by the Jmeter tool but in the same scenario, laravel has no race condition problem.
I want to ensure that. Is laravel handling race conditions on its own?
this is simple PHP:
<?php
if(empty($_GET['name'])){
exit('name is required');
}
try {
$conn = new PDO("mysql:host=localhost;dbname=databasename", 'user', 'pass');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?,?,?)");
$numRowsStmt = $conn->prepare("Select * from users where name = ?");
$numRowsStmt->execute([$_GET['name']]);
$numRows = $numRowsStmt->rowCount();
if($numRows <= 0){
$name = $_GET['name'];
$email = rand(100,50000) . '@gmail.com';
$pass = 'password';
$stmt->execute([$name,$email,$pass]);
exit('user created');
}
exit('user exists');
} catch(PDOException $e) {
echo $e->getMessage();
}
this is laravel:
Route::get('/100/{name}', function (Request $request, $name) {
$r = Validator::make(compact('name'), ['name' => 'unique:users,name']);
if ($r->fails()) {
exit('its exist');
};
User::create(['name' => $request->name, 'email' => rand(100, 80000) . '[email protected]', 'password' => IlluminateSupportFacadesHash::make(rand(100,50000)),]);
});
I know these codes are not clean I use this only to test what I want the other things are unnecessary. thanks.