connection->transaction(function () use ($database, $password) { // Lock the row to serialize concurrent rotations of the same database. $database->newQuery()->whereKey($database->getKey())->lockForUpdate()->firstOrFail(); $database->update([ 'password' => $this->encrypter->encrypt($password), ]); $this->dynamic->set('dynamic', $database->database_host_id); $this->repository->dropUser($database->username, $database->remote); $this->repository->createUser($database->username, $database->remote, $password, $database->max_connections); $this->repository->assignUserToDatabase($database->database, $database->username, $database->remote); $this->repository->flush(); }); return $password; } }