Home » Php » php – How do I manage/close connections if I can't change max_user_connections variable?

php – How do I manage/close connections if I can't change max_user_connections variable?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am using shared hosting that has “max user connections” set to 30. I’ve contacted them and they said they could not change this value. By surfing through my website I get an exception thrown that says:

SQLSTATE[42000]: Syntax error or access violation: 1203 User id12578877_odonto already has more than 'max_user_connections' active connections

Most of the actions in my web are just repositories (that have been generated with make:crud command) filtering some data. Upon changing between these pages rapidly for like 30s I get this error.

Is there any way I could reduce the number of connections or just basically anything I could do that would help?

Here is an example of how some of the actions in my controller look:

    /**
     * @Route("/", name="pacientai_index", methods={"GET"})
     */
    public function index(PacientaiRepository $pacientaiRepository): Response
    {
        if($this->getUser()==null){
            return $this->redirectToRoute('main');
        }
        if($this->getUser()->getRole()=="NEW"){
            return $this->redirectToRoute('main');
        }
        if($this->getUser()->getMiestas()=="London"){
            return $this->redirectToRoute('main');
        }
        return $this->render('pacientai/index.html.twig', [
            'pacientais' => $pacientaiRepository->findByStatus("NEW"),
            'sutarti' => $pacientaiRepository->findByStatus("SUTARTAS"),
        ]);
    }

    /**
     * @Route("/mine", name="pacientai_index_mine", methods={"GET", "POST"})
     */
    public function myPatients(PacientaiRepository $pacientaiRepository, UserRepository $userRepository): Response
    {
        if($this->getUser()==null){
            return $this->redirectToRoute('main');
        }
        if($this->getUser()->getRole()=="NEW"){
            return $this->redirectToRoute('main');
        }
        if($this->getUser()->getMiestas()=="London"){
            return $this->redirectToRoute('main');
        }
        $entityManager = $this->getDoctrine()->getManager();
        $entity = $this->getDoctrine()
            ->getRepository(User::class)
            ->find($this->getUser()->getId());
        $entity->setYraNauju(0);
        $entityManager->persist($entity);
        $entityManager->flush();
        return $this->render('pacientai/myPatients.html.twig');
    }

Am I doing something wrong here that could cause this?

I got this error while just testing with one user. Would it get even worse if, let’s say, 20 users were connected to the website?

If there are too may open connections, how could I make that all requests would be from one connection?

How to&Answers:

You can, after do whatever you need to do, do this:

$entityManager->getConnection()->close();

Or, if you have a class that handles that, you can actually put that code into the __destruct method, then when the garbage collector runs, it will also close the connection, like this:

public function __destruct()
{
   $entityManager = $this->getDoctrine()->getManager();
   $entityManager->getConnection()->close();
}