Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close connections and clear managers on shutdown #366

Merged
merged 1 commit into from
Feb 18, 2015

Conversation

stof
Copy link
Member

@stof stof commented Jan 4, 2015

When shutting down the kernel, the connections should be closed to avoid leaking connections during tests, which could reach the connection limit. The entity managers should also cleared to give more chances to entities to be garbage collected.
Closes Behat/Symfony2Extension#52

@Ocramius
Copy link
Member

Ocramius commented Jan 5, 2015

Is this just a matter of tests?
Can a small integration test be written?

What if I want to re-use a connection from a kernel after the kernel has been shut down (for example for late batch-processing of a 201 or 202 request)?

@Ocramius Ocramius added the Bug label Jan 5, 2015
@stof
Copy link
Member Author

stof commented Jan 9, 2015

@Ocramius in a web context, the kernel is never shut down. A kernel.terminate action happens after handling the request and sending the response, but not after shutdown

The main action of shutting down the kernel is removing the reference to the container, so that booting again recreates a new container (your existing container would ultimately be garbage collected but not immediately because of circular object graphs).

The goal of the shutdown is to reset your kernel instance entirely. the only use case I see is testing (to isolate your tests while keeping the same kernel reference).

When shutting down the kernel, the connections should be closed to avoid
leaking connections during tests, which could reach the connection
limit. The entity managers should also cleared to give more chances to
entities to be garbage collected.
Closes Behat/Symfony2Extension#52
@driesvints
Copy link

Just a FYI but I also encountered this when running unit tests. I'm not using Symfony but I'm using Laravel 4.2 + Doctrine (DBAL 2.5.1). A simple connection close and entity manager clear, like @stof suggested above solved the problem for me as well.

@oleg-andreyev
Copy link
Contributor

👍 need this.

@stof
Copy link
Member Author

stof commented Feb 18, 2015

I'm going to merge this one. Writing an integration test for the shutdown would be a pain

@stof stof merged commit 79bc1e4 into doctrine:master Feb 18, 2015
stof added a commit that referenced this pull request Feb 18, 2015
This PR was merged into the 1.3.x-dev branch.

Discussion
----------

Close connections and clear managers on shutdown

When shutting down the kernel, the connections should be closed to avoid leaking connections during tests, which could reach the connection limit. The entity managers should also cleared to give more chances to entities to be garbage collected.
Closes Behat/Symfony2Extension#52

Commits
-------

79bc1e4 Close connections and clear managers on shutdown
@stof stof deleted the close_connections branch February 18, 2015 09:39
@peterrehm
Copy link

This change creates issues on my side as per the following simplified test.

    public function testCloseRoute()
    {
        $task = $this->createTask();
        $this->callRoute('task_close', array('id' => $task->getId()));
        $this->assertEquals(302, $this->client->getResponse()->getStatusCode());
        $this->client->followRedirect();
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
        $this->removeEntity($task);
    }

After this change I am getting the error:

1) TaskBundle\Tests\Controller\TaskTest::testCloseRoute
Doctrine\ORM\ORMInvalidArgumentException: A detached entity was found during removed TaskBundle\Entity\Task@000000000d592bdf00000001251a1486

A $em->merge() is not working as the entity is stated as not found. What would you recommend as workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Too many connections - Doctrine connections are not closed
5 participants