Skip to content

Commit

Permalink
Merge pull request #3 from LordWeedlle/clear_close_shutdown_1.9.1
Browse files Browse the repository at this point in the history
Adding support for close and clear on shutdown
  • Loading branch information
sandvige authored Nov 5, 2018
2 parents 9a03dfd + f4489bc commit b59ef9e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 12 deletions.
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private function getDbalConnectionsNode()
->scalarNode('shard_choser')->end()
->scalarNode('shard_choser_service')->end()
->booleanNode('keep_slave')->end()
->booleanNode('close_on_shutdown')->defaultTrue()->end()
->arrayNode('options')
->useAttributeAsKey('key')
->prototype('scalar')->end()
Expand Down Expand Up @@ -484,6 +485,7 @@ private function getOrmEntityManagersNode()
->scalarNode('quote_strategy')->defaultValue('doctrine.orm.quote_strategy.default')->end()
->scalarNode('entity_listener_resolver')->defaultNull()->end()
->scalarNode('repository_factory')->defaultValue('doctrine.orm.container_repository_factory')->end()
->booleanNode('clear_on_shutdown')->defaultTrue()->end()
->end()
->children()
->arrayNode('second_level_cache')
Expand Down
6 changes: 4 additions & 2 deletions DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ protected function dbalLoad(array $config, ContainerBuilder $container)

$connections = [];

foreach (array_keys($config['connections']) as $name) {
foreach ($config['connections'] as $name => $connection) {
$connections[$name] = sprintf('doctrine.dbal.%s_connection', $name);
$container->setParameter($connections[$name] . '.close_on_shutdown', $connection['close_on_shutdown']);
}

$container->setParameter('doctrine.connections', $connections);
Expand Down Expand Up @@ -328,8 +329,9 @@ protected function ormLoad(array $config, ContainerBuilder $container)
}

$entityManagers = [];
foreach (array_keys($config['entity_managers']) as $name) {
foreach ($config['entity_managers'] as $name => $manager) {
$entityManagers[$name] = sprintf('doctrine.orm.%s_entity_manager', $name);
$container->setParameter($this->entityManagers[$name] . '.clear_on_shutdown', $manager['clear_on_shutdown']);
}
$container->setParameter('doctrine.entity_managers', $entityManagers);

Expand Down
30 changes: 20 additions & 10 deletions DoctrineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ public function boot()
$this->autoloader = Autoloader::register($dir, $namespace, $proxyGenerator);
}

private function clearManagerIfRequired($id)
{
if ($this->container->getParameter($id . '.clear_on_shutdown') === true) {
if (!method_exists($this->container, 'initialized') || $this->container->initialized($id)) {
$this->container->get($id)->clear();
}
}
}

private function closeConnectionIfRequired($id)
{
if ($this->container->getParameter($id . '.close_on_shutdown') === true){
if (!method_exists($this->container, 'initialized') || $this->container->initialized($id)) {
$this->container->get($id)->close();
}
}
}

/**
* {@inheritDoc}
*/
Expand All @@ -104,11 +122,7 @@ public function shutdown()
// Clear all entity managers to clear references to entities for GC
if ($this->container->hasParameter('doctrine.entity_managers')) {
foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
if (method_exists($this->container, 'initialized') && ! $this->container->initialized($id)) {
continue;
}

$this->container->get($id)->clear();
$this->clearManagerIfRequired($id);
}
}

Expand All @@ -118,11 +132,7 @@ public function shutdown()
}

foreach ($this->container->getParameter('doctrine.connections') as $id) {
if (method_exists($this->container, 'initialized') && ! $this->container->initialized($id)) {
continue;
}

$this->container->get($id)->close();
$this->closeConnectionIfRequired($id);
}
}

Expand Down
2 changes: 2 additions & 0 deletions Resources/config/schema/doctrine-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<xsd:attribute name="shard-choser" type="xsd:string" />
<xsd:attribute name="shard-choser-service" type="xsd:string" />
<xsd:attribute name="auto-commit" type="xsd:string" />
<xsd:attribute name="close-on-shutdown" type="xsd:string" default="true" />
<xsd:attribute name="schema-filter" type="xsd:string" />
<xsd:attribute name="logging" type="xsd:string" default="false" />
<xsd:attribute name="profiling" type="xsd:string" default="false" />
Expand Down Expand Up @@ -218,6 +219,7 @@
<xsd:attribute name="quote-strategy" type="xsd:string" />
<xsd:attribute name="entity-listener-resolver" type="xsd:string" />
<xsd:attribute name="repository-factory" type="xsd:string" />
<xsd:attribute name="clear-on-shutdown" type="xsd:string" default="true" />
<!-- deprecated attributes, use the child element instead -->
<xsd:attribute name="result-cache-driver" type="xsd:string" />
<xsd:attribute name="metadata-cache-driver" type="xsd:string" />
Expand Down
6 changes: 6 additions & 0 deletions Tests/DependencyInjection/AbstractDoctrineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function testDbalLoadSingleMasterSlaveConnection()
'host' => 'localhost',
'unix_socket' => '/path/to/mysqld.sock',
'defaultTableOptions' => [],
'close_on_shutdown' => true,
],
$param['master']
);
Expand Down Expand Up @@ -149,6 +150,7 @@ public function testDbalLoadPoolShardingConnection()
'host' => 'localhost',
'unix_socket' => '/path/to/mysqld.sock',
'defaultTableOptions' => [],
'close_on_shutdown' => true,
],
$param['global']
);
Expand Down Expand Up @@ -198,6 +200,7 @@ public function testLoadSimpleSingleConnection()
'driver' => 'pdo_mysql',
'driverOptions' => [],
'defaultTableOptions' => [],
'close_on_shutdown' => true,
],
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down Expand Up @@ -238,6 +241,7 @@ public function testLoadSimpleSingleConnectionWithoutDbName()
'driver' => 'pdo_mysql',
'driverOptions' => [],
'defaultTableOptions' => [],
'close_on_shutdown' => true,
],
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down Expand Up @@ -279,6 +283,7 @@ public function testLoadSingleConnection()
'dbname' => 'sqlite_db',
'memory' => true,
'defaultTableOptions' => [],
'close_on_shutdown' => true,
],
new Reference('doctrine.dbal.default_connection.configuration'),
new Reference('doctrine.dbal.default_connection.event_manager'),
Expand Down Expand Up @@ -527,6 +532,7 @@ public function testSingleEntityManagerDefaultTableOptions()

$param = $container->getDefinition('doctrine.dbal.default_connection')->getArgument(0);

$this->assertArrayHasKey('close_on_shutdown', $param);
$this->assertArrayHasKey('defaultTableOptions', $param);

$defaults = $param['defaultTableOptions'];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
<config>
<dbal close-on-shutdown="false" />
</config>
</srv:container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/doctrine"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
<config>
<orm clear-on-shutdown="false" />
</config>
</srv:container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
doctrine:
dbal:
close_on_shutdown: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
doctrine:
orm:
clear_on_shutdown: false

0 comments on commit b59ef9e

Please sign in to comment.